1,现在的vue项目是基于组件开发的单页面项目,并且通过路由来加载组件(所有后缀为.vue页面),所有的组件都会加载到index.html上来运行。
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>my_vue</title>
</head>
<body>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
2,vue项目不同于传统的web项目,他创意的引入了编译运行的概念,vue项目会被编译成成js项目来运行
和所有的编译后运行的语言,例如c#、java等创建的项目一样,vue项目同样具有入口函数,他是src目录下的main.js.
值得注意的是vue.js是可以直接在浏览器上运行的,必须打包运行的是vue的组件。
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'
import router from './router'
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
在此页面声明了一个vue项目,并且加载了一个app.vue的组件。
3,打开app.vue组件,其中 <router-view/>节点是声明,再此处加载路由的其他页面,那么app.vue就相当于一个公共母版页,类似于嵌套了一个ifrmame,不同于iframe的加载方式,组件会继承项目第一次加载的js和css等资源,而iframe是完全独立的页面,父页面和子页面没有资源冲突。
<template>
<div id="app">
<img src="./assets/logo.png">
<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>
4,router文件下的index.js,此js的功能是声明组件,并解析浏览器的地址跳转到不同的组件。
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
}
]
})
5,总结,项目初始化的时候index页面通过main.js,默认加载组件app.vue,并加载了监听路由的容器。