第一步:构建一个简单的vue项目,老规矩直接在命令行输入
vue init webpack myproject
cd my vue
cnpm/npm install
cnpm/npm run dev
执行结果以下
而后你会在8080端口看到vue的标志页面
第二步:分析目录结构 主要是组件入口app.vue和main.js
第三步:写页面
咱们在app.vue下这样写css
<template> <div id="wrapper"> <router-view></router-view> </div> </template> <script> export default { data () { return { } }, components: { } } </script>
在main.js中这样写vue
import Vue from 'vue' import App from './App' import Home from './pages/Home.vue' import VueRouter from 'vue-router' import 'bootstrap/dist/css/bootstrap.css' Vue.use(VueRouter) const routes = [{ path: '/', component: Home }] const router = new VueRouter({ routes }) /* eslint-disable no-new */ new Vue({ el: '#app', router, template: '<App/>', components: { App } })
main.js主要包括模块导入以及组件导入和注册,路由配置,固然路由配置能够单独写出来。
由上面的路由配置能够知道当path为‘/’时候,咱们渲染到app.vue中的页面为home.vue页面,以下jquery
<template> <div class="jumbotron"> <h1>这个是路由对应的页面,下面就是一个表格组件</h1> <table-com/> </div> </template> <script> import table from '../components/table.vue' export default { data () { return { } }, components: { tableCom: table } } </script>
其中import table from '../components/Hello.vue'表示导入这个table组件到home.vue页面
可是只导入而没有注册这个组件是无效的,就好像定义了一个函数而没有执行。因此咱们须要注册这个组件
也就是components:{tableCom: table}意思是自定义一个tableCom标签来映射这个组件,可是vue规定但标签名过长的时候,须要以分开方式去写好比tableCom 要写成table-com.
这样就完成了一个组件的导入和注册,下面咱们来完成这个组件
table.vue界面以下webpack
<template> <div style="padding:20px;" id="app"> <div class="panel panel-primary"> <div class="panel-heading">用户管理</div> <table class="table table-bordered table-striped text-center"> <thead> <tr> <th>序号</th> <th>用户名</th> <th>年龄</th> <th>毕业学校</th> <th>操做</th> </tr> </thead> <tbody> <tr v-for ="(user,index) in users"> <td>{{index+1}}</td> <td>{{user.name}}</td> <td>{{user.age}}</td> <td>{{user.school}}</td> <td><button v-on:click="remove(index)">remove</button></td> </tr> <tr> <td></td> <td><input type="text" id="name" v-model="user.name"/></td> <td><input type="text" id="age"v-model="user.age"/></td> <td><input type="text" id="school"v-model="user.school"/></td> <td><button @click="insert">insert</button></td> </tr> </tbody> </table> </div> </div> </template> <script> export default { name: 'hello', data () { return { user: {'name': '', 'age': '', 'school': ''}, users: [ {'name': '李磊', 'age': '25', 'school': '洛阳理工'}, {'name': '张成', 'age': '23', 'school': '桂林电子科技'}, {'name': '炼心', 'age': '22', 'school': '江西电子科技'} ] } }, methods: { insert: function () { this.users.push(this.user) }, remove: function (index) { this.users.splice(index, 1) } } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> h1, h2 { font-weight: normal; } ul { list-style-type: none; padding: 0; } li { display: inline-block; margin: 0 10px; } a { color: #42b983; } tr,th{ text-align:center; } </style>
这个组件实现了简单的增删功能,主要是对data数据的修改,咱们要明白,咱们日常使用的jquery只是对dom节点的操做,好比咱们要改变一个数据咱们就要首先获取dom而后经过jquery的方法来获取值,而vue则否则它是对data数据进行操做,数据双向绑定,数据改变则视图改变,一样视图改变则数据改变。
到最后咱们看到的效果是这样的
你们有什么问题能够联系我,或者留言
你们也许也发现了这样操做data太繁琐,有没有简单的方式呢,有,那就是vuex 就像一个仓库提供你须要的数据。下一章节我会开始对vuex的使用作个总结,但愿想了解更多的朋友关注我,谢谢大家的支持。web