vue-cli的構建+Vue的超級簡單實例(轉)


本文鏈接:https://blog.csdn.net/qq_34320300/article/details/78546141
☞ vue-cli的構建:
0.概要(精華):

 

 1 # 安裝vue-cli
 2 npm install -g vue-cli
 3 
 4 # 使用vue-cli初始化項目
 5 vue init webpack my-project
 6 
 7 # 進入到目錄
 8 cd my-project
 9 
10 # 安裝依賴
11 npm install
12 
13 # 開始運行
14 npm run dev

1.新建文件夾目錄。

進入D盤的work文件夾:

1 $ d:
2 $ cd: work


新建名為vueProject的文件夾、並查看和進入:

1 $ mkdir vueProject
2 $ dir
3 $ cd vueProject

結果圖:

 

 

 


2.檢測 node.js 和 npm 版本(安裝node.js和npm)

1 $ node -v
2 $ npm -v

 

 

 

3.安裝vue-cli腳手架工具,命令(執行要一段時間):

1 $ npm install -g vue-cli

 

 

 


4.新建一個vue的項目,名為vueDemo,命令(如果出現錯誤,可能是vue-cli沒有安裝好,重新安裝試試):

1 $ vue init webpack vuedemo

再根據參數名(解釋如下),選擇Y/n等。結果圖:

 

 

 


5.進入新建的vuedemo目錄、安裝依賴命令(需要等待一段時間,如果長時間沒有響應,就ctrl+c停止掉,然后再執行一次即可):

1 $ cd vueDemo
2 $ npm install

結果圖:

 

 

 


6.開始運行,命令:

1 $ npm run dev

結果圖(訪問的網址):

 

 

 注意:假如8080端口被占用,修改如下文件中的端口號即可。

 

 

 


7.訪問,在瀏覽器中輸入 localhost:端口號。
結果圖:

 

 

 


8.用WebStrom查看已構建的項目:

 

 

 


☞ 構建一個簡單的Vue導航欄菜單實例
1.新建組件文件夾(pages)及文件(index、userCenter、userInfo):

index.vue代碼:

 1 <template>
 2   <div>
 3     <p>這是首頁</p>
 4   </div>
 5 </template>
 6 
 7 <script>
 8   export default {}
 9 </script>
10 
11 <style scoped>
12   p{
13     display: block;
14     background: #ffe87c;
15   }
16 </style>

 

userCenter.vue代碼:

 1 <template>
 2     <div>
 3         <p>這是個人中心</p>
 4         <router-link to="/userCenter/userInfo">用戶信息</router-link>
 5         <router-view></router-view>
 6     </div>
 7 </template>
 8 
 9 <script>
10     export default {}
11 </script>
12 
13 <style scoped>
14     p{
15         display: block;
16         background: #d6e9c6;
17     }
18 </style > 

 

userInfo.vue代碼:

 1 <template>
 2   <div>
 3     <p>我的名字是:阿水12344</p>
 4   </div>
 5 </template>
 6 
 7 <script>
 8   export default {}
 9 </script>
10 
11 <style scoped>
12   p{
13     display: block;
14     background: #77FFFF;
15   }
16 </style>

 

2.在路由配置文件中,導入新建的組件。(index.js我們不用了)

 

 

router.js代碼:

 1 import Vue from 'vue'
 2 import Router from 'vue-router'
 3 import Hello from '../components/HelloWorld'
 4 import Index from '../pages/index'
 5 import UserCenter from '../pages/userCenter'
 6 import UserInfo from '../pages/UserInfo'
 7 
 8 Vue.use(Router)
 9 
10 export default new Router({
11     routes: [
12         {
13         path: '/',
14         name: 'Hello',
15         component: Hello
16         },
17         {
18         path: '/index',
19         name: 'index',
20         component: Index
21         },
22         {
23         path: '/userCenter',
24         name: 'userCenter',
25         component: UserCenter,
26             children: [
27                 {
28                 path: 'userInfo',
29                 name: 'userInfo',
30                 component: UserInfo
31                 }
32             ]
33         }
34     ]
35 })

 

3.在項目入口App.vue中建立導航欄,代碼如下:

 1 <template>
 2     <div id="app">
 3         <img src="./assets/logo.png">
 4         <p>這可以看做是導航欄</p>
 5         <router-link to="/index">首頁</router-link>
 6         <router-link to="/userCenter">個人中心</router-link>
 7         <router-view></router-view>
 8     </div>
 9 </template>
10 
11 <script>
12     export default {
13       name: 'app'
14     }
15 </script>
16 
17 <style>
18     #app {
19       font-family: 'Avenir', Helvetica, Arial, sans-serif;
20       -webkit-font-smoothing: antialiased;
21       -moz-osx-font-smoothing: grayscale;
22       text-align: center;
23       color: #2c3e50;
24       margin-top: 60px;
25     }
26 </style>

 

4.修改mian.js

1 import router from './router'

改為:

1 import router from './router/router.js'

main.js代碼:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router/router.js'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    template: '<App/>',
    components: { App }
})

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM