一、main.js
项目的入口文件
import Vue from 'vue'
//自动寻找名字为App的文件,如app.vue app.js...
import App from './App'
/* 引入router变量*/
import router from './router'
Vue.config.productionTip = false
new Vue({
/* 挂载在id=app的div元素上 */
el: '#app',
/* 使用router变量 router:router*/
router,
/* es6写法,为App:App 键值一样可以省写为一个。名字为App的局部组件*/
components: { App },
/* 将App局部组件显示在页面上*/
template: '<App/>'
})
二、app.vue
文件以vue后缀结尾,为单文件组件,其中为vue组件
<template>
/* 模板标签*/
<div id="app">
<img src="./assets/logo.png">
/* 显示的是当前路由地址对应的内容 即router目录下index.js引用的HelloWorld组件的内容*/
<router-view/>
</div>
</template>
<script>
/* 组件的逻辑*/
export default {
name: 'App'
}
</script>
<style>
/* 组件的css样式*/
#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>
三、路由
路由就是根据网址的不同,返回不同的内容给用户
如:url为 http://localhost:8080/#/list 返回列表页面
router目录下index.js
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
/* 当访问根目录时,展示HelloWorld组件*/
name: 'HelloWorld',
component: HelloWorld
}
]
})