書接上文我們說道,如何利用腳手架(vue-cli)構建一個vue項目忆矛,本回書我們一起來學習分析下代碼。
回顧下創(chuàng)建后的項目目錄:
說明:在*.vue文件漫拭,template標簽里寫html代碼,且template直接子級只能有一個標簽礼旅。style標簽里寫樣式痘系,script里面寫js代碼
a. 頁面:index.html
這個沒什么好說的就是一個簡單的html頁面汰翠,這里id='app'复唤,是為后面的設置vue作用域有關的。
<!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的意思是這里的樣式只對當前頁面有效不會影響其他頁面型豁,還有可以設置lang="scss"就是支持css預編譯充尉,也就是支持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">

<router-view></router-view> <!-- 這里是用來展示路由頁面內(nèi)容的驼侠,如果想用跳轉就用<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 相當于默認組件 */
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,這里是“@”相當于“../” */
Vue.use(Router) /* 使用路由 */
export default new Router({
routes: [ /* 進行路由配置,規(guī)定“/”引入到Hello組件 */
{
path: '/',
name: 'Hello', /* 這里的name同上周拐,暫時沒發(fā)現(xiàn)有什么意思 */
component: Hello /* 注冊Hello組件 */
}
]
})