標準前端化工程 Vue-cli

知識回顧

在學習Vue-cli之前,我們已經學習了前端體系、前後端分離的演變、Vue入門、Vue的基本語法、Vue組件、Vue路由、Axios等內容。接下來我們就來學習標磚前端化工程Vue-cli。

Vue-cli

什麼是Vue-cli?

Vue-cli是官方提供的一個腳手架工具,我們可以利用它快速生成前端化的工程模板,十分方便好用。
其功能主要有:

  • 統一的目錄
  • 快速調試
  • 單元測試
  • 在線運行

環境安裝

使用npm進行全局安裝,如果是首次安裝可能速度會有點慢。

npm install vue-cli -g

檢測我們安裝的Vue應用。

vue list

安裝和檢測Vue-cli

第一個Vue-cli前端程序

1、新建文件夾vue-cli。
2、創建一個基於webpack模板的Vue應用程序。

![image-20200401201939648.png](https://upload-images.jianshu.io/upload_images/11405558-fee9ccf56645d377.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
vue init webpack myvue

建議初學前端化工程的朋友除了Project name、Project description、Author和Vue build,其他問題全部選擇no!
創建基於webpack模板的前端化工程
3、安裝依賴(一般下載的前端工程是沒有任何依賴的),運行程序。

cd myvue #進入當前項目目錄
npm install #安裝所有依賴
npm run dev #啓動項目

4、程序啓動成功後,按住Ctrl鍵,鼠標單擊終端中顯示的http://localhost:8080,即可打開瀏覽器訪問網頁進行測試。
啓動成功時終端顯示內容
Vue-cli工程默認首頁
如上圖所示,現在我們已經創建了一個標準的前端化工程了,之後就基於這個工程進行操作了。

Vue-cli目錄結構分析

Vue-cli工程的目錄結構如下圖所示。
Vue-cli工程的目錄結構
主要目錄文件的作用:

  • build和config:webpack配置文件和項目配置文件。
  • node_modules:這個目錄一般在開源項目中都不存在,我們拿到項目的第一步就是安裝所有依賴(npm install)。
  • src:項目的源碼目錄(Vue項目和js代碼)。
  • static:靜態資源文件。
  • .babelrc:Babel配置文件(ES6語法轉換爲ES5語法)。
  • .editorconfig:編輯器配置。
  • .gitignore:git文件忽略配置。
  • .postcssrc.js:CSS相關的配置文件,即導入CSS插件。
  • index.html:首頁,所有的頁面都是通過這裏跳轉的,實際開發是不使用這個文件的。
  • package.json:項目的配置文件,包括名稱、版本、描述、作者、依賴、啓動腳本等。

src目錄

src就是我們編寫前端項目的源目錄,所有代碼都寫在這個目錄裏面。
src目錄結構

main.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.

// es6語法,導入組件和依賴!
import Vue from 'vue'    // vue 依賴
import App from './App'  // 導入組件

Vue.config.productionTip = false  // 關閉瀏覽器控制檯關於環境的提示!

/* eslint-disable no-new */
// vue的核心對象
new Vue({
  el: '#app',  // 節點
  components: { App },  // 組件
  template: '<App/>'  // 模板
})

App.vue

App.vue是配置路由跳轉的標準的Vue模板頁面。

<!-- HTML 代碼模板 -->
<template>
  <div id="app">
    <!-- 配置路由跳轉 -->
    <router-link to="/hello"></router-link>
    <!--顯示跳轉頁面的內容-->
    <router-view/>
    <HelloWorld/>
  </div>
</template>

<!--JS 代碼 -->
<script>
// JS 代碼, 導入我們自己寫的模塊!
import HelloWorld from './components/HelloWorld'
// 導入對象App,在其他地方導入的話就可以直接使用了!
export default {
  name: 'App',
  components: {
    HelloWorld  // 組件!
  }
}
</script>

<!--CSS 樣式: 如果沒有加 scoped 就是全局生效,如果增加了就是當前頁面生效!-->
<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

標準的路由

準備工作

vue-router是Vue的官方路由,可以和Vue無縫集成。
1、在項目中安裝vue-router。

npm install vue-router  --save-dev

安裝vue-router
2、在模板化工程中導入和使用它。

//導入我們的路由組件
import VueRouter from 'vue-router'
//顯式的調用Vue路由
Vue.use(VueRouter)

測試

1、清空項目中的多餘內容。
2、在Components目錄下創建和定義我們自己的組件。
定義組件

<template>
  <div>
    <h1>內容頁</h1>
  </div>
</template>

<script>
export default {
  name: 'Content'
}
</script>

3、在router目錄下的index.js中編寫安裝路由。
路由配置文件
4、編寫我們自己的路由組件。

// 導入Vue
import Vue from 'vue'
// 導入我們的路由組件
import VueRouter from 'vue-router'
// 顯示的調用Vue路由
Vue.use(VueRouter);

// 導入我們自己寫的組件, 不需要增加 .vue 後綴!
import Content from '../components/Content'
import Main from '../components/Main'

// 配置路由
export default new VueRouter({
  // 就是我們上週講的
  routes: [
    // 規則1 , content 內容頁跳轉規則!
    {
      path: '/content',
      name: 'content',
      component: Content
    },
    // 規則2
    {
      path: '/main',
      name: 'main',
      component: Main
    }
  ]
})

5、在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'

Vue.config.productionTip = false

// 導入我們的路由規則, 自動識別 index.js
import router from './router'

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router, // 掛載路由!這裏實際上相當於router:router,參數同名可省略
  components: { App },
  template: '<App/>'
})

6、在App.vue中使用即可。

<template>
  <div id="app">
    <router-link to="/main">首頁</router-link>
    <router-link to="/content">內容</router-link>
    <!-- 出口,展現路由內容的地方! -->
    <router-view></router-view>
  </div>
</template>

<script>

export default {
  name: 'App'
}
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

404配置(擴展)

1、編寫404.vue組件。

<template>
  <div>
    <h1>頁面不存在!404</h1>
  </div>
</template>

<script>
export default {
  name: 'NotFound'
}
</script>

2、在index.js中配置路由。

//省略export default new VueRouter({routers;[...]})
// 關於404,路由會優先匹配精準的,然後匹配通用!
{
    path: '*',//通配,但是會優先匹配NotFound組件
    component: NotFound
}

路由模式

Vue-cli工程有兩種路由模式:

  • hash:默認路由模式,路徑會帶#,如:http://localhost:8080/#/main
  • history:不帶#,就是我們常常看到的網頁路由,如:http://localhost:8080/main
    可以在index.js中配置路由模式。
export default new VueRouter({
  mode: 'history', // 配置路由模式!
  routes: []
}

配置完成後訪問網頁路徑中就不會有#了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章