javascript - 是否可以将对象中的 Vue/Javascript 代码发送到另一个组件?
问题描述
我有一个父子Vue
组件。子组件为父组件提供渲染页面所需的数据,该数据使用一个简单的对象发出,该对象使用emit
.
子组件数据:
const Steps [
{
sequence: 1,
name: "Personal",
description: `<p>Enter your name and phone number</p>`,
},
{
sequence: 2,
name: "OTP",
description: `<p>An OTP code has been sent to you. Resend code</p>`,
},
]
const SelectedStep = ref( 0 ); // Which step did you select?
const ActiveStep = ref( {} ); // What step is the form currently on?
SelectedStep.value += 1; // e.g. SelectedStep.value === 2
// Get the object in Steps array where the sequence === 2
ActiveStep.value = Steps.find(step => {
return step.sequence === SelectedStep.value
})
// Send this to the parent to render the description and title
emit('SelectedStep', ActiveStep.value);
根据sequence
所选择的,Steps
匹配该序列值的对象将被加载到ActiveStep
. 然后将其发射/提供给父组件。
但是,如果您查看sequence: 2
上面的对象,则其中description
是resend code
文本。我需要将其作为绑定链接,以便在单击它时运行一个函数以重新发送代码。我想象过这样的事情:
{
sequence: 2,
name: "OTP",
description: `<p>An OTP code has been sent to you. <a v-on:click="resendOTP">Resend code</a></p>`,
},
当它在页面上呈现时,v-on:click
它不会被解释并在 HTML 中按原样呈现。
父组件只是一个使用该组件的视图:
<header>
<h1>{{ActiveStep.title}}</h1>
<div v-html="`${ActiveStep.description}`">{{ActiveStep.description}}</div>
</header>
<div>
<div class="content">
<Component-Signup v-on:SelectedStep="updateActiveStep"/>
</div>
</div>
<script>
import ComponentSignup from "../../components/Signup.vue"
export default {
components: {
"Component-Signup": ComponentSignup
},
setup() {
const ActiveStep = ref({});
function updateActiveStep(SelectedStep) {
ActiveStep.value = SelectedStep // SelectedStep is the object emitted from child component
}
return {
updateActiveStep,
ActiveStep
}
}
}
</script>
这怎么可能实现?
解决方案
推荐阅读
- php - Yii2 DI 容器:两次注册同一个类,但构造函数参数不同
- firebase - 服务器到服务器身份验证失败 - firebase - dialogflow - google 上的操作 - 用户实体 v2
- angular - 以角度 5 显示动态创建的行的表格
- javascript - 自动重新排序 PDF 文档中的页面
- python - iteritems() DateFrame 输出机制和 append() 输出到一个新的 DataFrame
- vue.js - Vue JS动态组件单元测试挂载组件失败
- android - 在 onCreate 中使用 PersistableBundle 时,它显示了一个空白页
- java - 在firebase代码android中出现错误如何解决?
- sql - SQL Server:连接表和插入新数据时重复行?
- python - 如何并行化两个嵌套的for循环?