类型错误:无法读取未定义的属性'created',错误'Vue'未定义 Vue.js 3



我正试图从文档中使用vue.js 3中的自定义指令,我在文档中使用了这个示例,但我有以下错误:

  • 未定义"Vue">

并且当我移除";vue";从指令代码中,这些错误显示在控制台中:

  • 未捕获(在promise中(类型错误:无法读取未定义的属性"created">
  • 未捕获(在promise中(TypeError:无法读取null的属性"parentNode">

我认为这些错误是因为我使用了vue版本3,但我使用的是来自文档的vue版本中的自定义指令

main 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
}
})

组件:

<p v-highlight="'yellow'" class="content">{{ limit(content,90, 'more...') }}</p>

你能帮我吗

Vue 3中没有名为vue的对象或函数,只为您注册一个全局指令——应用程序实例已经定义为:

...
const app=createApp(App)
app.use(store).use(router).mount("#app");
app.directive('highlight', {
beforeMount(el, binding) {
el.style.background = binding.value
}
})

最新更新