简体   繁体   English

Nuxt mixin - 属性或方法未在实例上定义,但在渲染期间被引用

[英]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.

相关问题 Nuxt。 属性或方法“posts”未在实例上定义,但在渲染期间被引用 - Nuxt. Property or method "posts" is not defined on the instance but referenced during render Nuxt 属性或方法未在实例上定义,但在渲染期间被引用 - Nuxt property or method is not defined on the instance but referenced during render 未在实例上定义属性或方法(…),但在渲染期间引用了该属性或方法 - Property or method (…) is not defined on the instance but referenced during render 属性或方法“_”未在实例上定义,但在渲染期间引用 - Property or method "_" is not defined on the instance but referenced during render [Vue警告]:属性或方法未在实例上定义,但在渲染期间被引用 - [Vue warn]:Property or method is not defined on the instance but referenced during render 属性或方法“greet”未在实例上定义,但在渲染期间被引用 - Property or method “greet” is not defined on the instance but referenced during render VueJS:属性或方法......未在实例上定义,但在渲染期间被引用 - VueJS: Property or method ... is not defined on the instance but referenced during render 属性或方法“名称”未在实例上定义,但在渲染期间被引用 - Property or method "names" is not defined on the instance but referenced during render Vue:属性或方法“APP”未在实例上定义,但在渲染期间引用 - Vue: Property or method “APP” is not defined on the instance but referenced during render 未在实例上定义但在渲染期间引用的属性或方法 I Vuex - Property or method not defined on the instance but referenced during render I Vuex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM