【问题标题】:Vue router link button componentVue路由器链接按钮组件
【发布时间】:2020-01-08 09:28:42
【问题描述】:

我正在使用 Vue 构建简单的按钮组件。我正在尝试在组件中存储一个道具“链接”。

然后在父组件中我只想使用按钮组件,比如:

<btn :link="page link" />

我可以用简单的“a”标签来做到这一点,但我想用路由器推送来触发我的页面转换。也许我在编写组件时做错了什么。如果有人有不同的想法,我可以。

我需要一个按钮组件,在父组件中我将链接作为数据传递。

<script>
export default {
	props: {
		link: {
			type: String,
			required: false,
		},
	},
}
</script>
<template>
	<div class="btn" @click="$router.push(link)">
		<slot>btn text</slot>
	</div>
</template>

【问题讨论】:

  • 当使用router-link时,页面转换应该可以工作。
  • 是的,它工作,我只是不知道如何从父组件传递链接数据

标签: vue.js button components


【解决方案1】:

我不知道,但我在父级中传递链接数据时使用 ':' 并且应该没有它,因为我没有绑定任何内容。 所以工作代码现在看起来像这样

<script>
export default {
	props: {
		link: {
			type: String,
			required: true,
		},
	},
}
</script>
<template>
	<div class="btn" @click="$router.push(link)">
		<slot>btn text</slot>
	</div>
</template>

在父组件中我只是这样称呼它

<btn link="contact" />  // go to contact page

【讨论】:

  • 您使用: 将道具或属性绑定到脚本中定义的属性,如我编辑的答案中所述
猜你喜欢
  • 2018-02-10
  • 2022-01-17
  • 2019-05-20
  • 2018-04-30
  • 2019-02-27
  • 1970-01-01
  • 2019-11-11
  • 2021-11-22
  • 2019-04-11
相关资源
最近更新 更多