vue.js 学习——起步
来源:互联网 发布:淘宝商品自检 编辑:程序博客网 时间:2024/06/08 05:43
Vue.js 是一个数据驱动的js框架。最方便的一点就是,模板和数据是完全绑定的。不需要在timeout的时候调用$apply,或者监听store的事件,完全是自动更新的。Vue.js的一个基本用法是:
- 单页面
假设我们有一个object
var object = { message :'hello world'};还有一个template
<div id="app">{{message}}</div>通过vue将二者绑定在一起
new vue({ el: '#app', data:object});
- 作为Component
我们可以将templete 和data绑定在一个vue里面,然后注册为一个component里面,以后就可以在这个标签里面使用了。
var Example = new Vue({ template:'<div>{{message}}</div>', data:function(){ return:{ message:'hello world' } // 返回一个对象,跟 template里面的message绑定 }});Vue.component('example', Example);
以后就可以在<example></example>里面使用了。component 里面允许嵌套component。通过使用props标签来定义从父component接收数据。使用slot标签来确定使用父component自己的template来修改父component的内容。
。
- 模块化 modularity
模块化是前端的大趋势,vue可以自动把可选object转为component的构造器,因此可以简单的输出一个object。
// ./componentA.jsexport default{ template:‘<div>{{message}}</div>’, data(){ return { message:'hello world!' } }}<pre name="code" class="javascript">// 在app.js中import ComponentA from './componentA'export default{ // use another component, in this scope only. // ComponentA maps to the tag <component-a> Component:{ComponentA}, template:'<div> <p> using another component</p> <component-a></component-a>
</div> '}
同时 ,vue还支持把style,template,js 封装在一起export出去。
- 路由route
Vue类似于React本身并不提供路由,但是官方提供了插件可以使用。
import Vue from 'vue'import VueRouter from 'vue-router'import App from './app.vue'import ViewA from './view-a.vue'import ViewB from './view-b.vue'Vue.use(VueRouter)const router = new VueRouter()router.map({ '/a': { component: ViewA }, '/b': { component: ViewB }})router.start(App, '#app')
<div> <p>this is a layout won't change</p> <route-view></route-view></div>
一个但应用的例子:
<div id="app"><input v-model="newTodos" v-on:click="addTodo" /><ul><li v-for="todo in todos"> <span>{{todo.text}}</span> <button v-on:click="removeTodo($index)">x</button></li></ul></div><pre name="code" class="javascript">new Vue({ el:"#app", data:{ newTodos: '', todos:[{text:"hellow world"}] }, method:{ addTodo:function(){ var text = this.newTodos.trim(); if(text){ this.todos.push({text:text}); this.newTodos = ''; } }, removeTodo:function(index){ this.todos.splice(index,1); } }});
0 0
- vue.js 学习——起步
- Vue.js起步学习笔记
- vue.js 的起步
- vue.js起步
- Vue.js起步
- 【Vue】—Vue.js入门教程
- vue.js学习——实例分析
- Vue起步
- Vue.js学习总结(1)——Windows下Vue.js开发环境搭建
- Vue.js学习系列四——Webpack学习实践
- Vue.js学习系列一 —— vue-router2学习实践笔记(附DEMO)
- Vue.js学习系列一 —— vue-router2学习实践笔记(附DEMO)
- Vue.js学习系列一 —— vue-router2学习实践笔记(附DEMO)
- Vue.js学习系列六——Vue单元测试Karma+Mocha学习笔记
- Vue.js学习系列六——Vue单元测试Karma+Mocha学习笔记
- 【08】vue.js — vue实例
- 初学vue.js——学习笔记一
- Vue.js学习之路—环境配置
- 【BZOJ】2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛
- 嵌入式开发学习路线图
- Windows(Ubuntu)下GitBash生成public ssh key
- UITextField设置文字偏移的效果
- C语言,可变参数列表
- vue.js 学习——起步
- 选择和评测Linux VPS/服务器可采用的9个检测方法/工具
- Elasticsearch Term查询
- 程序设计与算法 第五周测验
- POJ 3067 Japan(经典树状数组)
- android 点9图片
- [编程题]最长公共子串
- 面试算法之字符串匹配算法,Rabin-Karp算法详解
- 傅里叶分析之掐死教程(完整版)更新于2014.06.06