javascript - Vue 3-mounted() 没有被组件调用?
问题描述
我有一个非常简单的 Vue 3 组件,它看起来与文档中的一些示例相同。代码如下:
// Typewriter.vue
<template>
<div id="wrapper">
<p>{{ text }}</p>
</div>
</template>
<script>
export default {
name: "typewriter",
props: {
phrase: {
type: String,
required: true,
},
},
data() {
return {
text: "",
};
},
mounted() {
console.log('Hello World!');
}
};
</script>
<style scoped></style>
然后我通过以下方式在另一个组件中渲染它:
<template>
<p>Blah</p>
<typewriter phrase="some text" />
</template>
<script>
import Typewriter from "@/components/Typewriter.vue";
export default {
name: "bio",
components: {
Typewriter
}
};
</script>
<style scoped></style>
但是,mounted()
似乎没有得到调用?如果我将其更改为,setup()
则方法内部的任何内容都会被调用,但这当然不适用于我的情况,因为我需要访问this
组件。
我错过了一些明显的东西吗?抱歉,Vue 和学习新手。
解决方案
事实证明,问题出在我遵循的教程中,明确地在 Webpack 构建中(我正在使用带有 webpacker 的 Rails 应用程序),设置以下内容:
...
environment.plugins.prepend(
"Define",
new DefinePlugin({
__VUE_OPTIONS_API__: false, // This is the problem
__VUE_PROD_DEVTOOLS__: false,
})
);
...
这当然会关闭选项 API 并强制您使用组合 API(我不是)。
推荐阅读
- javascript - 错误不必要的转义字符:\+ no-useless-escape
- c++ - 具有来自多个类的值的 C++ std::vector
- ios - 文本搜索防止在 Swift 中重新加载 tableview 中的所有数据
- windows - cplex API 包中的 configure.win 文件应该包含什么?
- mysql - SQL 查询另一个表中的值
- spring-webflux - Spring WebFlux Block Hound 告诉阻塞状态
- android - 如何使用密码保护本机蓝牙连接?
- java - Spring Boot R2DBC 无法在 MariaDB 中存储字符字段
- ios - 无法在调试模式设备上安装应用程序,也无法在 ad hoc 中安装应用程序。但是在模拟器中工作
- c++ - 使用两个向量时替代 CTRL+Z