[英]TypeError: Cannot read property 'created' of undefined, error 'Vue' is not defined Vue.js 3
i'm trying to use Custom Directives in vue.js 3 from the documentation, and i use the example that in the documentation but i've these errors:我正在尝试在文档中使用 vue.js 3 中的自定义指令,并且我使用了文档中的示例,但我有这些错误:
and when i remove "vue" from the Directive code these errors show in the console:当我从指令代码中删除“vue”时,这些错误会显示在控制台中:
I think these error cuz i use vue version 3 but i'm using Custom Directives that in Vue vesion 3 from documentation我认为这些错误是因为我使用 vue 版本 3,但我使用的是文档中 Vue vesion 3 中的自定义指令
main js:主要js:
import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import "bootstrap";
import "bootstrap/dist/css/bootstrap.min.css";
import "./scss/main.scss";
import "normalize.css";
createApp(App).use(store).use(router).mount("#app");
//Directive
const app = vue.createApp({})
app.directive('highlight', {
beforeMount(el, binding) {
el.style.background = binding.value
}
})
component:零件:
<p v-highlight="'yellow'" class="content">{{ limit(content,90, 'more...') }}</p>
Could you help me你可以帮帮我吗
There's no object or function called vue
in Vue 3, to register a global directive just you the app instance already defined like:在 Vue 3 中没有名为
vue
的 object 或 function 来注册一个全局指令,只需要你已经定义的应用程序实例,如:
...
const app=createApp(App)
app.use(store).use(router).mount("#app");
app.directive('highlight', {
beforeMount(el, binding) {
el.style.background = binding.value
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.