检查数据/道具是否传递?

发布于 2025-02-09 11:51:32 字数 430 浏览 2 评论 0原文

我得到了我传递一些数据的组件。我想检查是否传递“标题”,并有条件地设置其值。正如现在起作用的那样,我传递的价值刚刚覆盖。我该怎么做?

<Meta title="test" />

成分

<template>
  <vue-headful
    :title="this.title"
  />
 </template>
 <script>
import vueHeadful from "vue-headful";

export default {
  name: "Meta",
  components: { vueHeadful },
  data() {
    return {
      title: "default val"
    };
  }
};

I got a component that i pass in some data to. I want to check if "title" is passed or not, and conditionally set its value. As it works now, the value i pass just overrides. How would i do that?

<Meta title="test" />

Component

<template>
  <vue-headful
    :title="this.title"
  />
 </template>
 <script>
import vueHeadful from "vue-headful";

export default {
  name: "Meta",
  components: { vueHeadful },
  data() {
    return {
      title: "default val"
    };
  }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月流歌 2025-02-16 11:51:33

在您的元组件中,您不是定义道具,而是一个数据中的变量。您应该从数据中删除标题并添加一个prop:

props: 
   {title: {
      type: String,
      default: 'default val'
    }},

请参阅Props

您也不需要。在模板中标题之前。
&lt; vue-headful:title =“ title” /&gt; < /code>就足够了。

In your Meta component you are not defining the prop, but a variable in data. You should delete the title from data and add a prop:

props: 
   {title: {
      type: String,
      default: 'default val'
    }},

See documentation on props

You also do not need this. before title in your template.
<vue-headful :title="title" /> will be enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文