Vue2.0史上最全入坑教程(中)—— 腳手架代碼詳解

書接上文我們說道,如何利用腳手架(vue-cli)構(gòu)建一個vue項目,本回書我們一起來學(xué)習(xí)分析下代碼。

回顧下創(chuàng)建后的項目目錄:


說明:在*.vue文件,template標(biāo)簽里寫html代碼,且template直接子級只能有一個標(biāo)簽。style標(biāo)簽里寫樣式,script里面寫js代碼
a. 頁面:index.html

這個沒什么好說的就是一個簡單的html頁面,這里id='app',是為后面的設(shè)置vue作用域有關(guān)的。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>datura</title>
  </head>
  <body>
    <div id="app"></div>  
    <!-- built files will be auto injected -->
  </body>
</html>
b. 文件:Hello.vue
<template>
  <div class="hello">
    <h1>{{ msg }}</h1>  <!-- 這里是展示數(shù)據(jù)中的  -->
    <h2>Essential Links</h2>
    <ul>
      <li><a  target="_blank">Core Docs</a></li>
      <li><a  target="_blank">Forum</a></li>
      <li><a  target="_blank">Gitter Chat</a></li>
      <li><a  target="_blank">Twitter</a></li>
      <br>
      <li><a  target="_blank">Docs for This Template</a></li>
    </ul>
    <h2>Ecosystem</h2>
    <ul>
      <li><a  target="_blank">vue-router</a></li>
      <li><a  target="_blank">vuex</a></li>
      <li><a  target="_blank">vue-loader</a></li>
      <li><a  target="_blank">awesome-vue</a></li>
    </ul>
  </div>
</template>
<script>
export default {
  name: 'hello',   /* 這個name暫時不知道用啥用,根據(jù)官方文檔說的是方便排錯的 */
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'   /* 這里是數(shù)據(jù),一定記住數(shù)據(jù)一定要放data中然后用return返回 */
    }
  }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>   /*  scoped的意思是這里的樣式只對當(dāng)前頁面有效不會影響其他頁面,還有可以設(shè)置lang="scss"就是支持css預(yù)編譯,也就是支持sass或者less  */
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

c. 文件:App.vue
<template>
  <div id="app">
    ![](./assets/logo.png)
    <router-view></router-view>   <!--  這里是用來展示路由頁面內(nèi)容的,如果想用跳轉(zhuǎn)就用<router-link to='xxx'></router-link> -->
  </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>
d. 文件:main.js

這個js文件是主頁面配置的主入口。主要是利用es6的模塊化引入模塊。

import Vue from 'vue'   /* 這里是引入vue文件 */
import App from './App'  /* 這里是引入同目錄下的App.vue模塊 */
import router from './router'  /* 這里是引入vue的路由 */

/* eslint-disable no-new */
new Vue({
  el: '#app',  /* 定義作用范圍就是index.html里的id為app的范圍內(nèi) */
  router,    /* 引入路由 */
  template: '<App/>',   /* 給Vue實例初始一個App組件作為template 相當(dāng)于默認(rèn)組件 */
  components: { App }  /* 注冊引入的組件App.vue */
})

e. 文件:index.js

這個是配置路由的頁面

import Vue from 'vue'   /* 引用vue文件 */
import Router from 'vue-router'  /* 引用vue路由模塊,并賦值給變量Router */
import Hello from '@/components/Hello'  /* 英文Hello.vue模版,并賦值給變量Hello,這里是“@”相當(dāng)于“../” */
Vue.use(Router)   /* 使用路由 */
export default new Router({
  routes: [     /* 進(jìn)行路由配置,規(guī)定“/”引入到Hello組件 */
    {
      path: '/',
      name: 'Hello',  /* 這里的name同上,暫時沒發(fā)現(xiàn)有什么意思 */
      component: Hello   /* 注冊Hello組件 */
    }
  ]
})
說明:如果需要增加組件那就在components文件下定義xx.vue文件并編寫代碼即可,如果需要配置路由就要進(jìn)行在index.js進(jìn)行路由“路徑”配置,還需要點擊跳轉(zhuǎn)就要用到<router-link></router-link>標(biāo)簽了。至于后面的過濾器啊,事件啊,鉤子函數(shù),ajax等等之類的和vue1.0差不多就不一一敘述,但是會在用到的時候簡單說明一下的。我會用下面大約倆個章節(jié)來展示一個簡單的“小項目”。

Vue2.0史上最全入坑教程(下)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容