我如何使用條件來更改這些道具的價值?在組件中,如果沒有傳遞,我想將 props 值設定為另一個。例如,如果沒有傳遞標題,我想將其設定為一些靜態字串。我該怎么做?
零件:
<template>
<vue-headful
:title="this.title"
:description="this.description"
:url="this.url"
:image="this.image"
/>
</template>
<script>
import vueHeadful from "vue-headful";
export default {
name: "Meta",
components: { vueHeadful },
props: {
title: String,
description: String,
url: String,
image: String
}
};
這是我在另一個檔案中使用它的方式:
<Meta title="test" />
uj5u.com熱心網友回復:
您可以為每個道具設定默認值:
export default {
name: "Meta",
components: { vueHeadful },
props: {
title: {
type: String,
default: ''
},
description: {
type: String,
default: 'foo'
},
url: {
type: String,
default: 'www.example.com'
},
image: {
type: String,
default: '/some/path'
}
}
};
您在檔案中有很多有趣的道具設定:https ://vuejs.org/guide/components/props.html#prop-validation
uj5u.com熱心網友回復:
您可以使用計算屬性:
https://vuejs.org/guide/essentials/computed.html
uj5u.com熱心網友回復:
您可以像這樣設定默認值title:
{
title: {
type: String,
default: 'YOUR DEFAULT TITLE GOES HERE'
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/493592.html
