有人知道如何在 Vue 3 中添加全局变量吗?
在 Vue 2 中,我们在main.js文件中使用它:
Vue.prototype.$myGlobalVariable = globalVariable
Run Code Online (Sandbox Code Playgroud) 有人知道如何在函数中使用mapState或mapGetters使用吗?我知道可以使用带有钩子的商店但是使用这个钩子我们导入所有商店,或者我们可以指定一个:Vue 3setupuseStoremapStatemapGettersmodule
// ...
computed: {
...mapGetters('myModule', [
'myStateVariable'
]
)
//...
Run Code Online (Sandbox Code Playgroud) 有人知道如何提供设置函数中的变量吗?
export default {
name: "MyComponent",
provide: {
myVariableThatIWantToProvide // This is not working
},
setup() {
const myVariableThatIWantToProvide = ref('test');
return {
myVariableThatIWantToProvide
};
}
};
</script>
Run Code Online (Sandbox Code Playgroud) 我想使用可组合项测试组件useNuxtApp。这是组件( MyComponent.vue):
<template>
<div class="flex justify-between">
<span>{{ $fmt(12) }}</span>
</div>
</template>
<script lang="ts" setup>
const { $fmt } = useNuxtApp()
</script>
Run Code Online (Sandbox Code Playgroud)
$fmt是文件夹中的插件plugins。
问题是当我尝试MyComponent.vue使用进行测试时vitest,测试未启动并且出现此错误:
ReferenceError: useNuxtApp is not defined
Run Code Online (Sandbox Code Playgroud)
我不知道如何模拟可组合useNuxtApp项
我刚刚检查了路由器视图(在 Vue 3 中)没有捕获从子级发送的事件。例如:
<router-view
@event-test="$emit('new-test-event')"
/>
Run Code Online (Sandbox Code Playgroud)
在这里,我的孩子发送event-test但router-view从未捕获它,因此new-test-event从未发出。