有什么方法可以将 Vue 自定义指令实现为单独的文件(ala Vue 组件 .vue 文件)并在根 js 文件中编译?(我正在使用 Webpack)
我在 app.js 上尝试了以下内容,但无法识别。
require('./directives/test.js');
Run Code Online (Sandbox Code Playgroud)
提前致谢。
指令只是一个具有一些定义良好的方法的类。在 JS 中,你可以这样写:
export const MyDirective {
bind(el,binding,vnode) {
/* your code */
}
}
Run Code Online (Sandbox Code Playgroud)
然后,在您使用它的文件中:
<template>
<p v-app-my-directive:arg.modifier="value">Some Text</p>
</template>
<script>
import MyDirective from './directives/MyDirective.js';
export default {
directives: {
AppMyDirective: MyDirective
}
/* ... */
}
</script>
Run Code Online (Sandbox Code Playgroud)
你也可以使用
Vue.directive('app-my-directive', MyDirective)
Run Code Online (Sandbox Code Playgroud)
全局声明它。
pea*_*man -1
好吧,我正在使用 Vue 和 Laravel,我会给你适合我的东西。
在你的 app.js (根文件)中将组件(或指令)注册为全局 Vue 组件。
所以: // 导入Vue
Vue = require('vue');
Vue.component('my-custom-component', require('./directives/ComponentFileName.vue')
Run Code Online (Sandbox Code Playgroud)
之后你就可以在 html 中使用它了,如下所示:
<my-custom-component></my-custom-component>
Run Code Online (Sandbox Code Playgroud)
如果我误解了你的问题,我很抱歉。
| 归档时间: |
|
| 查看次数: |
8793 次 |
| 最近记录: |