Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
、先從一個(gè)簡(jiǎn)單的例子開(kāi)始吧,創(chuàng)建一個(gè)空白文件test.html,添加vue庫(kù)鏈接。
二、這里創(chuàng)建一個(gè)全局子組件和一個(gè)父組件,父組件綁定vmdiv。
三、在vmdiv下創(chuàng)建一個(gè)div,并添加一個(gè)點(diǎn)擊事件。
四、于是我們用同樣的方法,也給子組件添加一個(gè)點(diǎn)擊事件,發(fā)現(xiàn)居然點(diǎn)擊沒(méi)有效果。
五、所以子組件的事件不同于原生元素里添加事件,在子組件里要添加一個(gè).native。
、深度作用選擇器( >>> )
嚴(yán)格來(lái)說(shuō),這個(gè)應(yīng)該是vue-loader的功能。”vue-loader”: “^12.2.0”
在項(xiàng)目開(kāi)發(fā)中,如果業(yè)務(wù)比較復(fù)雜,特別像中臺(tái)或B端功能頁(yè)面都不可避免的會(huì)用到第三方組件庫(kù),產(chǎn)品有時(shí)會(huì)想對(duì)這些組件進(jìn)行一些UI方面的定制。如果這些組件采用的是有作用域的CSS,父組件想要定制第三方組件的樣式就比較麻煩了。
深度作用選擇器( >>> 操作符)可以助你一臂之力。
上面的child組件中 .child-title 的作用域CSS設(shè)定字體大小為12px,現(xiàn)在想在父組件中定制為大小20px,顏色為紅色。
效果妥妥的。但是別高興太早,注意到上面的style使用的是純css語(yǔ)法,如果采用less語(yǔ)法,你可能會(huì)收到一條webpack的報(bào)錯(cuò)信息。
<style lang="less">
.parent-custom {
>>> .child-title {
font-size:20px;
color: red;
}
}
</style>
ERROR in ./~/css-loader!./~/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-960c5412","scoped":false,"hasInlineConfig":false}!./~/postcss-loader!./~/less-loader!./~/vue-loader/lib/selector.js?type=styles&index=0!./src/components/parent.vue
Module build failed: Unrecognised input
@ /src/components/parent.vue (line 22, column 6)
near lines:
.parent-custom {
>>> .child-title {
font-size:20px;
上面的報(bào)錯(cuò)信息其實(shí)是less語(yǔ)法不認(rèn)識(shí) >>>。(less的github issue上有人提議支持>>>操作符,但本文使用的v2.7.3會(huì)有這個(gè)問(wèn)題)
解決方案是采用的less的轉(zhuǎn)義(scaping)和變量插值(Variable Interpolation)
對(duì)于其他的css預(yù)處理器,因?yàn)闆](méi)怎么用,不妄加評(píng)論,照搬一下文檔的話(huà)。
有些像 Sass 之類(lèi)的預(yù)處理器無(wú)法正確解析 >>>。這種情況下你可以使用 /deep/ 操作符取而代之——這是一個(gè) >>> 的別名,同樣可以正常工作。
二、組件配置項(xiàng)inheritAttrs、組件實(shí)例屬性$attrs和$listeners
2.4.0新增
組件配置項(xiàng) inheritAttrs
我們都知道假如使用子組件時(shí)傳了a,b,c三個(gè)prop,而子組件的props選項(xiàng)只聲明了a和b,那么渲染后c將作為html自定義屬性顯示在子組件的根元素上。
如果不希望這樣,可以設(shè)置子組件的配置項(xiàng) inheritAttrs:false,根元素就會(huì)干凈多了。
組件實(shí)例屬性$attrs和$listeners
先看看vm.$attrs文檔上是怎么說(shuō)的
vm.$attrs
歸納起來(lái)就是兩點(diǎn):
vm.$attrs是組件的內(nèi)置屬性,值是父組件傳入的所有prop中未被組件聲明的prop(class和style除外)。
還是以前面的child組件舉例
組件可以通過(guò)在自己的子組件上使用v-bind=”$attrs”,進(jìn)一步把值傳給自己的子組件。也就是說(shuō)子組件會(huì)把$attrs的值當(dāng)作傳入的prop處理,同時(shí)還要遵守第一點(diǎn)的規(guī)則。
vm.$listeners
歸納起來(lái)也是兩點(diǎn):
1、vm.$listeners是組件的內(nèi)置屬性,它的值是父組件(不含 .native 修飾器的) v-on 事件監(jiān)聽(tīng)器。
2、組件可以通過(guò)在自己的子組件上使用v-on=”$listeners”,進(jìn)一步把值傳給自己的子組件。如果子組件已經(jīng)綁定$listener中同名的監(jiān)聽(tīng)器,則兩個(gè)監(jiān)聽(tīng)器函數(shù)會(huì)以冒泡的方式先后執(zhí)行。
//parent.vue
<template>
<div>
<child @update="onParentUpdate"></child>
</div>
</template>
<script>
export default {
name: 'parent',
components:{
Child
},
methods:{
onParentUpdate(){
console.log('parent.vue:onParentUpdate')
}
}
}
</script>
//child.vue
<template>
<div>
<grand-child @update="onChildUpdate" v-on="$listeners"></grand-child>
</div>
</template>
<script>
export default {
name: 'child',
components:{
GrandChild
},
methods:{
onChildUpdate(){
console.log('child.vue:onChildUpdate')
}
}
}
</script>
//grandchild.vue
<script>
export default {
name: 'grandchild',
mounted(){
//控制臺(tái)輸出:
//grandchild:$listeners: {update: ?}
console.log('grandchild:$listeners:',this.$listeners);
//控制臺(tái)輸出:
//child.vue:onChildUpdate
//parent.vue:onParentUpdate
this.$listeners.update();
}
}
</script>
三、組件選項(xiàng) provide/inject
在2.5.0+時(shí)對(duì)于inject選項(xiàng)為對(duì)象時(shí),還可以指定from來(lái)表示源屬性,default指定默認(rèn)值(如果是非原始值要使用一個(gè)工廠方法)。
四、作用域插槽 slot-scope
可以看出列表頁(yè)和編輯頁(yè)對(duì)于數(shù)據(jù)的展示是一樣的,唯一的區(qū)別是在不同頁(yè)面對(duì)于數(shù)據(jù)有不同的處理邏輯。相同的數(shù)據(jù)展示這塊就可抽取成一個(gè)組件,不同的地方則可以借助作用域插槽實(shí)現(xiàn)。
五、Vue的錯(cuò)誤捕獲
全局配置errorHandler
生命周期鉤子errorCaptured
如果熟悉React的話(huà),會(huì)發(fā)現(xiàn)它跟錯(cuò)誤邊界(Error Boundaries)的概念很像,實(shí)際上也確實(shí)是這么用的。
在文檔Error Handling with errorCaptured Hook就舉了一個(gè)典型的例子
需要強(qiáng)調(diào)的是errorCaptured并不能捕獲自身錯(cuò)誤和異步錯(cuò)誤(比如網(wǎng)絡(luò)請(qǐng)求,鼠標(biāo)事件等產(chǎn)生的錯(cuò)誤)。
參考
一、先創(chuàng)建一張空白網(wǎng)頁(yè)index.html,在head標(biāo)簽里設(shè)置好vue的鏈接庫(kù)。
二、寫(xiě)一個(gè)綁定元素div,一個(gè)子組件,一個(gè)父組件,父組件與元素div綁定。
三、我們寫(xiě)子組件的模板,并在div內(nèi)調(diào)用它,看看是否有效果。
四、那我們注冊(cè)子組件吧。
五、我們先試一下,父組件怎么把數(shù)據(jù)傳給子組件。:tocmp就是父組件的要傳給子組件的數(shù)據(jù),子組件用props來(lái)接收。
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。