<style> [v-cloak]{ visibility: hidden; } </style>
<div id="example"> {{ message.split('').reverse().join('') }} </div>
<p>Reversed message: "{{ reversedMessage() }}"</p> // 在組件中 methods: { reversedMessage: function () { return this.message.split('').reverse().join('') } }
a: function (val, oldVal) { console.log('new: %s, old: %s', val, oldVal) }
c: { deep:true, handler:function (val, oldVal) { console.log('new: %s, old: %s', val, oldVal) } }
num:{ immediate:true, handler:function(val){ this.nums = val*2 } }
xm:{ get:function(){//getter 當依賴改變後設置值的時候 return this.xing+'丶'+this.ming }, set:function(val){//setter 當自身改變後執行 this.xing = val.split('丶')[0] this.ming = val.split('丶')[1] } }
<p>{{msg | firstUpper(3,2)}}</p> Vue.filter('firstUpper',function (value,num=1,num2) { console.log(num2) return value.substr(0,num).toUpperCase()+value.substr(num).toLowerCase() })
filters:{ firstUpper:function (value,num=1,num2) { console.log(num2) return value.substr(0,num).toUpperCase()+value.substr(num).toLowerCase() } }
<input type="text" v-model="mode"> <template v-if="mode=='A'"> <h1>1.title</h1> <p>個人第一個P標籤</p> </template> <template v-else-if="mode=='B'"> <h1>2.title</h1> <p>個人第二個P標籤</p> </template> <template v-else-if="mode=='C'"> <h1>3.title</h1> <p>個人第三個P標籤</p> </template> <template v-else> <p>很差意思,輸入有誤</p> </template>
Vue.extend(options)
var App = Vue.extend({ template:"<h1>hello world</h1>" }) Vue.component('my-app',App)
// 建立組件構造器和註冊組件合併一塊兒 Vue.component('hello',{//Vue會自動的將此對象給Vue.extend template:"<h1>hello</h1>" })
new Vue({ el:"#app", components:{ 'my-app':App } })
data:{}, components:{ 'hello':{ template:"<h1>asdasdasdasdasdas</h1>" } }
<table id="app"> <tr is="hello"></tr> </table>
<template id="my-hello"> <div> <h1>hello world</h1> <p>hahahah</p> </div> </template> //組件中 template:"#my-hello"
<component :is="type"></component> //組件中 data:{ type:'aaa' }, components:{ 'aaa':{template:"<h1>AAAAAAAAAAAAA</h1>"}, 'bbb':{template:"<h1>BBBBBBBBBBBBB</h1>"} }
<bbb money="2"></bbb>
'bbb':{ props:['money'] }
<bbb clothes-logo='amani' clothes-price="16.58"></bbb> props:['clothesLogo','clothesPrice']
props: ['initialCounter'], data: function () { return { counter: this.initialCounter } } //定義一個計算屬性,處理 prop 的值並返回: props: ['size'], computed: { normalizedSize: function () { return this.size.trim().toLowerCase() } }
props:{ //類型驗證: str:String, strs:[String,Number], //必傳驗證 num:{ type:Number, required:true }, //默認數據 bool:{ type:Boolean, // default:true, default:function(){ return true } }, //自定義驗證函數 nums:{ type:Number, validator: function (value) { return value %2 == 0 } } }
<aaa>abc</aaa> template:"<h1><slot></slot></h1>"
<my-button>提交</my-button> <my-button>重置</my-button> <my-button></my-button> template:"<button><slot>按鈕</slot></button>"
<transition leave-active-class="animated fadeOut" enter-active-class="animated slideInLeft"> <p v-if="isShow" class="box"></p> </transition>
在vue中咱們能夠不用template來指定組件的模板,而是用render函數來建立虛擬dom結構,用這種方法優勢就是性能高,缺點就是使用成本高,代碼可讀性較低,可使用jsx來在render函數中建立,這樣既提升了性能,又減小了成本 可是,咱們在使用了vue-cli腳手架以後,由於腳手架中有對template標籤轉換虛擬dom的處理,因此,不須要使用jsx,咱們也能高效的轉換爲createElement形式
全局安裝 vue-cli npm install --global vue-cli 建立一個基於 webpack 模板的新項目 vue init webpack my-project //init以後能夠定義模板的類型 安裝依賴,走你 cd my-project npm install npm run dev
{ test: /\.scss$/, loader:'style-loader!css-loader!sass-loader' }
import Vue from 'vue' import Router from 'vue-router' Vue.use(Router)
new Router(options)
var routes = [ {path,component}//path爲路徑,component爲路徑對應的路由組件 ] new Router({ routes })
new Vue({ el: '#app', router, template: '<App/>', components: { App } })
<router-link to="main">main</router-link> <router-link to="news">news</router-link> .router-link-active{ color:red; }
const routes = [ {path:'/main',component:AppMain}, {path:'/news',component:AppNews,children:[ {path:'inside',component:AppNewsInside}, {path:'outside',component:AppNewsOutside} ]}, ]
<router-link to='inside'>inside</router-link> <router-link to='outside'>outside</router-link> <router-view></router-view>
{path:'',component:'/main'}
{path:'',redirect:'/main'} {path:'**',redirect:'/main'}
{path:'/user/:id',component:User}
:to='{name:"detail",params:{id:_new.id},query:{content:_new.content}}'
router.push = router-link:to router.replace = router-link:to.replace router.go() = window.history.go
router.beforeEach((to, from, next) => { //會在任意路由跳轉前執行,next必定要記着執行,否則路由不能跳轉了 console.log('beforeEach') console.log(to,from) next() }) router.afterEach((to, from) => { //會在任意路由跳轉後執行 console.log('afterEach') })
routes: [ { path: '/foo', component: Foo, beforeEnter: (to, from, next) => { // ... } } ]
beforeRouteEnter (to, from, next) { // 在渲染該組件的對應路由被 confirm 前調用 // 不!能!獲取組件實例 `this` // 由於當守衛執行前,組件實例還沒被建立 }, beforeRouteUpdate (to, from, next) { // 在當前路由改變,可是該組件被複用時調用 // 舉例來講,對於一個帶有動態參數的路徑 /foo/:id,在 /foo/1 和 /foo/2 之間跳轉的時候, // 因爲會渲染一樣的 Foo 組件,所以組件實例會被複用。而這個鉤子就會在這個狀況下被調用。 // 能夠訪問組件實例 `this` }, beforeRouteLeave (to, from, next) { // 導航離開該組件的對應路由時調用 // 能夠訪問組件實例 `this` }
<router-view class="view one"></router-view> <router-view class="view two" name="a"></router-view> <router-view class="view three" name="b"></router-view>
const router = new VueRouter({ routes: [ { path: '/', components: { default: Foo,//默認的,沒有name的router-view a: Bar, b: Baz } } ] })
{path:'detail/:id',component:AppNewsDetail,name:'detail',props:true}
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) //能夠設置store管理的state/getter,mutations,actions const store = new Vuex.Store({ })
import state from './modules/state' //能夠設置store管理的state/getter,mutations,actions const store = new Vuex.Store({ state })
import store from './store' new Vue({ el: '#app', router, store, template: '<App/>', components: { App } })
data(){ return { num:this.$store.state.num } }
computed:{ num(){ return this.$store.state.num } }
computed:mapState(['num'])
computed:mapState({ // _num:'num',//鍵名爲別名,值字符串表明的是真正的狀態 _num(state){//方法名爲別名,函數體裏還能夠對真正的狀態作出一些處理 return state.num } })
computed:{ a(){ return num+1 }, ...mapState({ // _num:'num',//鍵名爲別名,值字符串表明的是真正的狀態 _num(state){//方法名爲別名,函數體裏還能夠對真正的狀態作出一些處理 return state.num } }), }
const getters = { doublenum(state){ return state.num*2 } }
const mutations = { increment(state){ state.num++ } }
const actions = { [CHANGE_NUM]({commit}){ alert(1) setTimeout(() => { let num = Math.floor(Math.random()*10) //調用mitations的方法 commit(CHANGE_NUM,num) }, 1000); } }
function foo(){// 第16行 getName = function(){console.log(1)} return this } foo.getName = function(){console.log(2)} foo.prototype.getName = function(){console.log(3)} var getName = function(){console.log(4)} function getName(){console.log(5)} foo.getName()//2 //foo是一個函數,也能夠說是一個對象,因此它也能夠掛載一些屬性和方法,18行在其上掛載了一個getName方法 //執行的結果是 getName()//4 //21行有一個全局函數,全局函數聲明提早後被20行的getName覆蓋,因此輸出4 foo().getName()//1 //foo()執行完成後,將全局的getName也就是window.getName給更改後返回this,而在這裏this執行的就是window,因此最後執行的就是window.getName,因此輸出1 getName()//1 //在上面已經更改全局的getName,因此依然是1 new foo.getName()//2 //new 操做符在實例化構造器的時候,會執行構造器函數,也就是說,foo.getName會執行,輸出2 new foo().getName()//3 //new操做符的優先級較高,因此會先new foo()獲得一個實例,而後再執行實例的getName方法,這個時候,實例的構造器裏沒有getName方法,就會執行構造器原型上的getName方法 new new foo().getName()//3 //先執行new foo()獲得一個實例,而後在new 這個實例的getName方法,這個時候會執行這個方法,因此輸出3 //除了本地對象的方法,其餘的函數都能new