简体   繁体   English

如何在道具验证器中访问“this”

[英]how to access "this" in props validator

I'm working on a project using nuxt.js, I'm injecting a function in the context of the application as recommended in the official documentation我正在使用 nuxt.js 开发一个项目,我正在按照官方文档中的建议在应用程序的上下文中注入一个函数

https://nuxtjs.org/guide/plugins/#inject-in-root-amp-context https://nuxtjs.org/guide/plugins/#inject-in-root-amp-context

but when I try to call the function inside a props validation I get an error但是当我尝试在道具验证中调用该函数时,出现错误

/plugins/check-props.js

import Vue from 'vue'

Vue.prototype.$checkProps = function(value, arr) {
  return arr.indexOf(value) !== -1
}

in a component vue在组件 Vue 中

export default {
  props: {
    color: {
      type: String,
      validator: function (value, context) {
        this.$checkProps(value, ['success', 'danger'])
      }
  }
}

ERROR: Cannot read property '$checkProps' of undefined ERROR:无法读取未定义的属性“$checkProps”

Does anyone know how I can access "this" within validation?有谁知道我如何在验证中访问“这个”?

thanks in advance!提前致谢!

Props validation is done before the component is initialized, so you won't have access to this as you are extending Vue.prototype . Props 验证是在组件初始化之前完成的,因此您在扩展Vue.prototype时将无法访问this

Form their documentation :形成他们的文件

Note that props are validated before a component instance is created, so instance properties (eg data, computed, etc) will not be available inside default or validator functions.请注意,在创建组件实例之前会验证 props,因此实例属性(例如数据、计算等)在默认或验证器函数中将不可用。

In general, if $checkProps is only used for checking the value of these props, I would just use a helper function.一般来说,如果$checkProps仅用于检查这些 props 的值,我只会使用辅助函数。

// array.helpers.js
export function containsValue(arr, val) {
  return arr.indexOf(value) !== -1
}

// component
import { containsValue } from 'path/to/helpers/array.helpers';
props: {
    foo: {
       //
       validator(value) {
          return containsValue(['foo', 'bar'], value);
       }
    }
}

Update更新

Based on your comments, if you don't want to import this specific function over and over again, you can just Array.prototype.includes see docs根据您的评论,如果您不想一遍又一遍地导入此特定功能,您可以Array.prototype.includes 查看文档

// component
props: {
    color: {
       //
       validator(value) {
          return ['success', 'danger'].includes(value);
       }
    }
}

From the doc :文档

props are validated before a component instance is created, so instance properties (eg data, computed, etc) will not be available inside default or validator functions在创建组件实例之前验证 props,因此实例属性(例如数据、计算等)在默认或验证器函数中不可用

If you want to access the nuxt plugins you can always use the window object.This is how I do it to access the i18n library如果你想访问nuxt插件,你总是可以使用window对象。这就是我访问i18n库的方法

{
   validator: (value: any): boolean => {
      return window.$nuxt.$te(value);
   }
}

In your case:在你的情况下:

{
   validator: (value: any): boolean => {
      return window.$nuxt.$checkProps(value, ['success', 'danger']);
   }
}

In any case you should never prototype in the window object.在任何情况下,您都不应该在 window 对象中创建原型。 Let nuxt handle it with a plugin:nuxt用一个插件来处理它:

path: plugins/check-props.js or .ts路径: plugins/check-props.js.ts

function checkProps(value: any, arr: string[]): boolean {
  return arr.indexOf(value) !== -1
}

const $checkProps: Plugin = (_context: Context, inject: Inject) => {
  inject('checkProps', checkProps);
};

export default $checkProps;

And then in nuxt config然后在 nuxt 配置中

{
  plugins: [{ src: 'plugins/check-props.js', ssr: true }]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM