vue-cli搭建SPA項目


1. 什么是vue-cli?
vue-cli是vue.js的腳手架,用於自動生成vue.js+webpack的項目模板,創建命令如下:
vue init webpack xxx

 

 

2. 安裝vue-cli
npm install -g vue-cli

注1:安裝成功后,會出現如下文件
d:\tools ==> 根據自行安裝的目錄來操作
node-v10.15.3-win-x64
node_global
vue
vue.cmd
vue-init
vue-init.cmd
vue-list
vue-list.cmd

 


注2:安裝完成之后打開命令窗口並輸入 vue -V(注意這里是大寫的“V”),如果出現相應的版本號,則說明安裝成功。

 

 

一問一答”模式(見注2)

注1:cmd命令行窗口顯示中文亂碼,多是因為cmd命令行窗口字符編碼不匹配導致
修改cmd窗口字符編碼為UTF-8,命令行中執行:chcp 65001
切換回中文:chcp 936
這兩條命令只在當前窗口生效,重啟后恢復之前的編碼。

注2:“一問一答”模式
1.Project name:項目名,默認是輸入時的那個名稱spa1,直接回車
2.Project description:項目描述,直接回車
3.Author:作者,隨便填或直接回車
4.Vue build:選擇題,一般選第一個
4.1Runtime + Compiler: recommended for most users//運行加編譯,官方推薦,就選它了
4.2Runtime-only: about 6KB lighter min+gzip, but templates (or any Vue-specific HTML) are ONLY allowed in .vue files
- render functions are required elsewhere//僅運行時,已經有推薦了就選擇第一個了
5.Install vue-router:是否需要vue-router,Y選擇使用,這樣生成好的項目就會有相關的路由配置文件
6.Use ESLint to lint your code:是否用ESLint來限制你的代碼錯誤和風格。N 新手就不用了,但實際項目中一般都會使用,這樣多人開發也能達到一致的語法
7.Set up unit tests:是否安裝單元測試 N
8.Setup e2e tests with Nightwatch?:是否安裝e2e測試 N
9.Should we run `npm install` for you after the project has been created? (recommended) (Use arrow keys)
> Yes, use NPM
Yes, use Yarn
No, I will handle that myself //選擇題:選第一項“Yes, use NPM”是否使用npm install安裝依賴

全部選擇好回車就進行了生成項目,出現如下內容表示項目創建完成
# Project initialization finished!
# ========================

實在不會選,就回車選擇“默認”或是選擇“N”不安裝

 

## 步驟二:運行完上面的命令后,我們需要將當前路徑改變到SPA這個文件夾內,然后安裝需要的模塊
## 此步驟可理解成:maven的web項目創建成功后,修改pom文件添加依賴
cd spa1 #改變路徑到spa1文件夾下
npm install #安裝所有項目需要的npm模塊

 



## 步驟三:啟動並訪問項目
## 此步驟可理解成:啟動tomcat,並通過瀏覽器訪問項目
cd spa1
npm run dev

項目啟動成功后,打開瀏覽器輸入“http://localhost:35252”即可


4. package.json詳解
每個項目的根目錄下面,一般都有一個package.json文件,定義了這個項目所需要的各種模塊,
以及項目的配置信息(比如名稱、版本、許可證等元數據)。npm install命令根據這個配置文件,
自動下載所需的模塊,也就是配置項目所需的運行和開發環境
詳情見資料“package-詳解.json”中的相關注釋

 

6. vue項目結構說明

build文件夾 這個文件夾主要是進行webpack的一些配置
webpack.base.conf.js webpack基礎配置,開發環境,生產環境都依賴
webpack.dev.conf.js webpack開發環境配置
webpack.prod.conf.js webpack生產環境配置
build.js 生產環境構建腳本
vue-loader.conf.js 此文件是處理.vue文件的配置文件

config文件夾
dev.env.js 配置開發環境
prod.env.js 配置生產環境
index.js 這個文件進行配置代理服務器,例如:端口號的修改

node_modules文件夾 存放npm install時根據package.json配置生成的npm安裝包的文件夾

src文件夾 源碼目錄(開發中用得最多的文件夾)
assets 共用的樣式、圖片
components 業務代碼存放的地方,里面分成一個個組件存放,一個頁面是一個組件,一個頁面里面還會包着很多組件
router 設置路由
App.vue vue文件入口界面
main.js 對應App.vue創建vue實例,也是入口文件,對應webpack.base.config.js里的入口配置

static文件夾 存放的文件不會經過webpack處理,可以直接引用,例如swf文件如果要引用可以在webpack配置
對swf后綴名的文件處理的loader,也可以直接將swf文件放在這個文件夾引用

package.json 這個文件有兩部分是有用的:scripts 里面設置命令以及在dependencies和devDependencies中,
分別對應全局下載和局部下載的依賴包

8. 綜合案例
首頁-用戶中心-用戶
路由嵌套,關鍵代碼:
{
path: '/UserCenter',
name: 'UserCenter',
component: UserCenter,
children: [{
path: 'UserInfo',
name: 'UserInfo',
component: UserInfo,
}]
}
網站公共部分:頭部(header.vue)和腳部(footer.vue)的創建和使用

注1:以 " / " 開頭的嵌套路徑會被當作根路徑,所以子路由的 path 不需要添加 " / "

AppMain.vue

 

<template>
  <div class="hello">
    <h1>{{ ts }}</h1>
    <p>
            <router-link to="/Center">用戶中心</router-link>
            <router-link to="/About">關於我們</router-link>
        </p>
  </div>
</template>

<script>
export default {
  name: 'AppMain',
  data () {
    return {
      ts: new Date().getTime(),
            
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

 

 About.vue

 

<template>
    <div>
        <h1>關於我們</h1>
    </div>
</template>

<script>
    export default {
        name:'About',
        data:function(){
            return {
                
            }
        }
    }
</script>

<style>

</style>

 

Centenr.vue

 

<template>
    <div>
        <h1>用戶中心</h1>
        <p>
            <router-link to="/Center/Register">用戶注冊</router-link>
            <router-link to="/Center/UpdatePwd">修改密碼</router-link>
        </p>
        <div>
            <router-view></router-view>
        </div>
    </div>
</template>

<script>
    export default { 
        name:'Center',
        data:function(){
            return {
                
            }
        }
    }
</script>

<style>

</style>

 

Register.vue

 

<template>
    <div>
        <h1>用戶注冊</h1>
    </div>
</template>

<script>
    export default {
        name:'Register',
        data:function(){
            return {
                
            }
        }
    }
</script>

<style>

</style>

 

UpdatePwd.vue

 

<template>
    <div>
        <h1>修改密碼</h1>
    </div>
</template>

<script>
    export default {
        name:'UpdatePwd',
        data:function(){
            return {
                
            }
        }
    }
</script>

<style>

</style>

 

index.js

 

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import AppMain from '@/components/AppMain'
import About from '@/views/About'
import Center from '@/views/Center'
import Register from '@/views/Register'
import UpdatePwd from '@/views/UpdatePwd'


Vue.use(Router)
export default new Router({
  routes: [
    {
      path: '/HelloWorld',
      name: 'HelloWorld',
      component: HelloWorld
    },
        {
            path: '/',
            name: 'AppMai',
            component: AppMain
        },
        {
            path: '/About',
            name: 'About',
            component: About
        },
        {
            path: '/Center',
            name: 'Center',
            component: Center,
            children:[
                {
                    path: 'Register',
                    name: 'Register',
                    component: Register
                },
                {
                    path: 'UpdatePwd',
                    name: 'UpdatePwd',
                    component: UpdatePwd
                }
            ]
        },
        
  ]
})

 

 

 

 

 


免責聲明!

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



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