vue全局注冊是每個實例化的vue都可以使用,而局部則是實例化注冊的那個可以用。舉個例子,看看寫法:
<div id="app">
<p>頁面載入時,input 元素自動獲取焦點:</p>
<input v-focus>
</div>
<script> // 注冊一個全局自定義指令 v-focus
Vue.directive('focus', {
// 當綁定元素插入到 DOM 中。
inserted: function (el) {
// 聚焦元素
el.focus() } })
// 創建根實例
new Vue({ el: '#app' })
</script>
局部注冊:
我們也可以在實例使用 directives 選項來注冊局部指令,這樣指令只能在這個實例中使用:
<div id="app">
<p>頁面載入時,input 元素自動獲取焦點:</p>
<input v-focus>
</div>
<script> // 創建根實例
new Vue({
el: '#app',
directives: {
// 注冊一個局部的自定義指令 v-focus
focus: {
// 指令的定義
inserted: function (el) {
// 聚焦元素
el.focus() } } }
})
</script>
