W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
is
prop 用法僅限于保留的 <component>
標(biāo)記。v-is
指令來支持 2.x 用例,其中在原生元素上使用了 v-is
來處理原生 HTML 解析限制。如果我們想添加在 Vue 外部定義的自定義元素 (例如使用 Web 組件 API),我們需要“指示”Vue 將其視為自定義元素。讓我們以下面的模板為例。
<plastic-button></plastic-button>
在 Vue 2.x 中,將標(biāo)記作為自定義元素白名單是通過 Vue.config.ignoredElements
:
// 這將使Vue忽略在Vue外部定義的自定義元素
// (例如:使用 Web Components API)
Vue.config.ignoredElements = ['plastic-button']
在 Vue 3.0 中,此檢查在模板編譯期間執(zhí)行指示編譯器將 <plastic-button>
視為自定義元素:
isCustomElement
傳遞給 Vue 模板編譯器,如果使用 vue-loader
,則應(yīng)通過 vue-loader
的 compilerOptions
選項(xiàng)傳遞: // webpack 中的配置
rules: [
{
test: /\.vue$/,
use: 'vue-loader',
options: {
compilerOptions: {
isCustomElement: tag => tag === 'plastic-button'
}
}
}
// ...
]
app.config.isCustomElement
傳遞: const app = Vue.createApp({})
app.config.isCustomElement = tag => tag === 'plastic-button'
需要注意的是,運(yùn)行時(shí)配置只會(huì)影響運(yùn)行時(shí)模板編譯——它不會(huì)影響預(yù)編譯的模板。
自定義元素規(guī)范提供了一種將自定義元素用作自定義內(nèi)置模板的方法,方法是向內(nèi)置元素添加 is
屬性:
<button is="plastic-button">點(diǎn)擊我!</button>
Vue 對(duì) is
特殊 prop 的使用是在模擬 native attribute 在瀏覽器中普遍可用之前的作用。但是,在 2.x 中,它被解釋為渲染一個(gè)名為 plastic-button
的 Vue 組件,這將阻止上面提到的自定義內(nèi)置元素的原生使用。
在 3.0 中,我們僅將 Vue 對(duì) is
屬性的特殊處理限制到 <component>
tag。
<component>
tag 上使用時(shí),它的行為將與 2.x 中完全相同; <foo is="bar" />
bar
組件。is
prop 渲染 foo
組件。is
選項(xiàng)傳遞給 createElement
調(diào)用,并作為原生 attribute 渲染,這支持使用自定義的內(nèi)置元素。 <button is="plastic-button">點(diǎn)擊我!</button>
plastic-button
組件。 document.createElement('button', { is: 'plastic-button' })
v-is
用于 DOM 內(nèi)模板解析解決方案提示:本節(jié)僅影響直接在頁面的 HTML 中寫入 Vue 模板的情況。 在 DOM 模板中使用時(shí),模板受原生 HTML 解析規(guī)則的約束。一些 HTML 元素,例如
<ul&
,<ol&
,<table&
和<select&
對(duì)它們內(nèi)部可以出現(xiàn)的元素有限制,和一些像<li&
,<tr&
,和<option&
只能出現(xiàn)在某些其他元素中。
在 Vue 2 中,我們建議通過在原生 tag 上使用 is
prop 來解決這些限制:
<table>
<tr is="blog-post-row"></tr>
</table>
隨著 is
的行為變化,我們引入了一個(gè)新的指令 v-is
,用于解決這些情況:
<table>
<tr v-is="'blog-post-row'"></tr>
</table>
WARNING
v-is
函數(shù)像一個(gè)動(dòng)態(tài)的 2.x :is
綁定——因此,要按注冊(cè)名稱渲染組件,其值應(yīng)為 JavaScript 字符串文本:
<!-- 不正確,不會(huì)渲染任何內(nèi)容 -->
<tr v-is="blog-post-row"></tr>
<!-- 正確 -->
<tr v-is="'blog-post-row'"></tr>
config.ignoredElements
與 vue-loader
的 compilerOptions
(使用 build 步驟) 或 app.config.isCustomElement
(使用動(dòng)態(tài)模板編譯)<component>
tags 與 is
用法更改為 <component is="...">
(對(duì)于 SFC 模板) 或 v-is
(對(duì)于 DOM 模板)。Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: