Vue2.0 脚手架代码详解

时间:2023-03-09 17:29:38
Vue2.0 脚手架代码详解

参考作者:https://www.jianshu.com/p/2b661d01eaf8

只是为了方便个人学习。

来看一下脚手架创建后的项目目录

Vue2.0 脚手架代码详解

 说明:在*.vue文件,template标签里写html代码,且template直接子级只能有一个标签。style标签里写样式,script里面写js代码

1. main.js

这个js文件是主页面配置的主入口。主要是利用es6的模块化引入模块

 // 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' /* 这里是引入vue文件 */
import App from './App'/* 这里是引入同目录下的App.vue模块 */
import router from './router'/* 这里是引入vue的路由 */ Vue.config.productionTip = false /* eslint-disable no-new */
new Vue({
el: '#app',/* 定义作用范围就是index.html里的id为app的范围内 */
router,/* 引入路由 */
components: { App },/* 给Vue实例初始一个App组件作为template 相当于默认组件 */
template: '<App/>'/* 注册引入的组件App.vue */
})

2. 文件:App.vue

 <template>
<div id="app">
<img src="./assets/logo.png">
<router-view/> <!-- 这里是用来展示路由页面内容的,如果想用跳转就用<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>

3.Hello.vue页面

 <template>
<div class="hello">
<h1>{{ msg }}</h1><!-- 这里是展示数据中的 -->
<h2>Essential Links</h2>
<ul>
<li>
<a
href="https://vuejs.org"
target="_blank"
>
Core Docs
</a>
</li>
<li>
<a
href="https://forum.vuejs.org"
target="_blank"
>
Forum
</a>
</li>
<li>
<a
href="https://chat.vuejs.org"
target="_blank"
>
Community Chat
</a>
</li>
<li>
<a
href="https://twitter.com/vuejs"
target="_blank"
>
Twitter
</a>
</li>
<br>
<li>
<a
href="http://vuejs-templates.github.io/webpack/"
target="_blank"
>
Docs for This Template
</a>
</li>
</ul>
<h2>Ecosystem</h2>
<ul>
<li>
<a
href="http://router.vuejs.org/"
target="_blank"
>
vue-router
</a>
</li>
<li>
<a
href="http://vuex.vuejs.org/"
target="_blank"
>
vuex
</a>
</li>
<li>
<a
href="http://vue-loader.vuejs.org/"
target="_blank"
>
vue-loader
</a>
</li>
<li>
<a
href="https://github.com/vuejs/awesome-vue"
target="_blank"
>
awesome-vue
</a>
</li>
</ul>
</div>
</template> <script>
export default {
name: 'HelloWorld',
data () {
/* 这里是数据,一定记住数据一定要放data中然后用return返回 */
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</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>
4. router文件下的index.js是路由配置

这个是配置路由的页面

 import Vue from 'vue' //这里是引用vue文件
import Router from 'vue-router' //这里是引入vu路由模块,并赋值给Router
import HelloWorld from '@/components/HelloWorld'///* 英文Hello.vue模版,并赋值给变量Hello,这里是“@”相当于“../” */ Vue.use(Router)/* 使用路由 */ export default new Router({
routes: [/* 进行路由配置,规定“/”引入到Hello组件 */
{
path: '/',
name: 'HelloWorld',
component: HelloWorld/* 注册Hello组件 */
}
]
})
如果需要增加组件那就在components文件下定义xx.vue文件并编写代码即可,如果需要配置路由就要进行在index.js进行路由“路径”配置,

注意:import from和export defalut的使用