简体   繁体   English

如何将变量传递给 vue.js 模板中的函数?

[英]How to pass variables to function inside of vue.js template?

I am trying to use a template component in Vue to change the heading of a page to the page title.我正在尝试使用 Vue 中的模板组件将页面标题更改为页面标题。 I am trying to do this by having all of the possible page titles stored in an array in the header-component file.我试图通过将所有可能的页面标题存储在header-component文件的数组中来做到这一点。 However, I cannot find a way to pass in an index when the component is loaded onto a page.但是,当组件加载到页面上时,我找不到传递索引的方法。

This is the component in question:这是有问题的组件:

<template id="comp-dem-template">
    <header-component>
        <!-- Insert replacement text in here-->
        <span slot="pagetitle">
            {{ chooseTitle(index) }}
        </span>
    </header-component>
</template>

<script>
module.exports = {
    template: '<h1><slot name="pagetitle">Page Title Fallback</slot></h1>',
    data: function chooseTitle(index) {
        if (index == 0){
            title: 'Index';
        }
        else if (index == 1){
            title: 'Events';
        }
        else if (index == 2){
            title: 'Policy';
        }
        else if (index == 3){
            title: 'Frequently Asked Questions';
        }
        else if (index == 4){
            title: 'Reservations';
        }
        else if (index == 5){
            title: 'View Reservations';
        }
        else{
            title: 'Make a Reservation';
        }
    }
}
</script>

<style>


</style>

And where the component loads from以及组件从哪里加载

<template>
  <div class="container">
      <logo />
      <headercomponent />
      <navbar/>
  </div>
</template>

<script>
import Logo from '~/components/Logo.vue'
import headercomponent from '~/components/header-component.vue'
import navbar from '~/components/nav-bar.vue'
export default {
  components: {
    Logo,
    headercomponent,
    navbar
  }
}
</script>

<style>

</style>

I want to be able to pass in a "0" somewhere on my Index page, causing the heading to have the text of "Index" , "1" causes "Events" , etc. Is this even possible, or am I barking up the wrong tree?我希望能够在我的索引页面的某处传递一个“0”,导致标题有“索引”的文本,“1”导致“事件”等。这是否可能,或者我在咆哮错误的树?

Try to declare that index as a prop in headercomponent as follows :尝试将该index声明为 headercomponent 中的 prop,如下所示:

<template id="comp-dem-template">
    <header-component>
        <!-- Insert replacement text in here-->
        <span slot="pagetitle">
            {{ title }}
        </span>
    </header-component>
</template>

<script>
module.exports = {
 template: '<h1><slot name="pagetitle">Page Title Fallback</slot></h1>',
 props:['index'],
 data:  {
   if (this.index == 0) { //use this keyword
     title: 'Index';
   }
....

and pass it from the parent component to the child one like:并将其从父组件传递给子组件,例如:

<template>
  <div class="container">
      <logo />
      <headercomponent index="0" />
      <navbar/>
  </div>
</template>

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

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