[英]Nuxt mixin - Property or method is not defined on the instance but referenced during render
I'm creating a Nuxt application, where a specific menu should be hidden on mobile.我正在创建一个 Nuxt 应用程序,其中应在移动设备上隐藏特定菜单。 So i've created a mixin plugin that has the property
isSmallScreen
which can be false
or true.所以我创建了一个 mixin 插件,它的属性
isSmallScreen
可以是false
或 true。
mixins.client.js mixins.client.js
import Vue from 'vue'
import styles from '@/assets/styles/base/globals/_responsive.scss'
const debug = true
let breakpoint = parseInt(styles.breakpoint, 10)
Vue.mixin({
data: function() {
return {
isSmallScreen: null
}
},
created() {
this.isSmallScreen = (window.innerWidth <= breakpoint)
}
})
I've registered the mixins plugin in nuxt.config.js
我已经在
nuxt.config.js
注册了 mixins 插件
plugins: [
'~/plugins/base/global/mixins.client.js',
]
Now I expect isSmallScreen
to be globally available.现在我希望
isSmallScreen
是全球可用的。 When I console.log this.isSmallScreen
in the mounted hook in layouts/default.vue
, it returns true
for small screens, and false
for bigger screens.当我在
layouts/default.vue
的挂载钩子中 console.log this.isSmallScreen
时,它对于小屏幕返回true
,对于大屏幕返回false
。 That seems to work fine.这似乎工作正常。
The problem问题
My default.vue layout template looks like我的 default.vue 布局模板看起来像
<template>
<div>
<client-only>
<div class="nav-container">
<f-nav-admin />
<f-nav-top v-if="!isSmallScreen"/>
</div>
</client-only>
<!-- page content -->
<div class="page-content-container">
<nuxt />
</div>
</div>
</template>
I expect the f-nav-top
component do appear on large screens, and hide on small screens.我希望
f-nav-top
组件确实出现在大屏幕上,并隐藏在小屏幕上。 Which also seems to work.这似乎也有效。
But ..但 ..
Even though the functionality does what it should I still get the warning as shown below.即使功能做了它应该做的事情,我仍然会收到如下所示的警告。
Property or method "isSmallScreen" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.
I've been looking for a solution for a while now, but can't find the solution.我一直在寻找解决方案一段时间了,但找不到解决方案。 Does anyone see what I'm doing wrong here?
有没有人看到我在这里做错了什么?
Thanks in advance提前致谢
I've found the problem.我找到了问题所在。 Inside the
<nuxt/>
component was a reference to isSmallScreen
as well. <nuxt/>
组件内部也有对isSmallScreen
的引用。 On page reload, the property didn't exists on the data though.在页面重新加载时,数据上不存在该属性。 That's because I named my mixins file
mixins.client.js
那是因为我将我的 mixins 文件命名为
mixins.client.js
In the documentation there is a part about Name conventional plugin which means that file.client.js
will only run on the client, and file.server.js
only SSR.在文档中有一部分是关于Name 常规插件的,这意味着
file.client.js
只会在客户端上运行,而file.server.js
只会在 SSR 上运行。 So that's why the data from my mixin plugin was not available which resulted in the warning.所以这就是为什么我的 mixin 插件中的数据不可用导致警告的原因。
Changing my mixins filename to mixins.js
did the fix for me.将我的 mixins 文件名更改为
mixins.js
为我做了修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.