简体   繁体   English

Vue.js 中的 Static 道具验证

[英]Static props validation in Vue.js

When using props validation in Vue, it only shows errors during the runtime in the browser console.在 Vue 中使用 props 验证时,它只会在运行时在浏览器控制台中显示错误。

But I want to get props validation to work during the static code analysis phase (linting).但我希望在 static 代码分析阶段(linting)期间进行道具验证。

For example, I have used the default Vue 2 project template:例如,我使用了默认的 Vue 2 项目模板:

HelloWorld.vue is the component that has msg prop and it's required: HelloWorld.vue是具有msg属性的组件,它是必需的:

<template>
  <h1>Message: {{ msg }}</h1>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: {
      type: String,
      required: true,
    },
  },
};
</script>

App.vue is using HelloWorld component, but doesn't specify msg prop. App.vue正在使用HelloWorld组件,但没有指定msg属性。 Instead, it uses non-existent prop something :相反,它使用不存在的道具something

<template>
  <HelloWorld :something="somewhat" />
</template>

<script>
import HelloWorld from './components/HelloWorld.vue';

export default {
  name: 'App',
  components: {
    HelloWorld,
  },
};
</script>

I expect at least some of these requirements to be met when I run npm run lint :我希望在运行npm run lint时至少能满足其中一些要求:

  • it should say Error: required "msg" prop not found它应该说Error: required "msg" prop not found
  • it should say Warning: unknown "something" prop has been used它应该说Warning: unknown "something" prop has been used
  • also, if I assign something other than String to msg prop, it should say Error: prop "msg" should be String, but received *NotString*另外,如果我将String以外的内容分配给msg prop,它应该会显示Error: prop "msg" should be String, but received *NotString*

In other words, I want Vue templates to be lintable, like JSX templates in React.换句话说,我希望 Vue 模板是 lintable 的,就像 React 中的 JSX 模板一样。

Is there any way I can achieve this in Vue 2 or Vue 3?有什么方法可以在 Vue 2 或 Vue 3 中实现这一点? Perhaps, some eslint plugins?也许,一些 eslint 插件?

If this can be solved using TypeScript - that would be super awesome.如果这可以使用 TypeScript 来解决 - 那将是非常棒的。

A solution to this problem, which works in both Vue 2 and Vue 3 is to use JSX (TSX).在 Vue 2 和 Vue 3 中都适用的解决方案是使用 JSX (TSX)。

Here's a Vue 3 example:这是一个 Vue 3 示例:

HelloWorld.tsx : HelloWorld.tsx

import { defineComponent } from "vue";

export default defineComponent({
  name: "HelloWorld",
  props: {
    msg: {
      type: String,
      required: true,
    },
  },
  setup: (props) => () => <h1>{props.msg}</h1>,
});

App.tsx : App.tsx

import { defineComponent } from "vue";
import HelloWorld from "./components/HelloWorld";

export default defineComponent({
  name: "App",
  render: () => <HelloWorld msg={"123"} />,
});

Here's Vue 2 example repo: https://github.com/Maxim-Mazurok/vue-2-tsx-example这是 Vue 2 示例存储库: https://github.com/Maxim-Mazurok/vue-2-tsx-example

And Vue 3: https://github.com/Maxim-Mazurok/vue-3-tsx-example和 Vue 3: https://github.com/Maxim-Mazurok/vue-3-tsx-example

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

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