简体   繁体   English

将属性参数传递给Vue组件

[英]Pass a property parameter to the Vue component

I'm using Vue+Laravel, I want to make a modal window with 2 tabs. 我正在使用Vue + Laravel,我想制作一个带有2个标签的模态窗口。 I have two buttons, each button must open certain connected tab. 我有两个按钮,每个按钮必须打开某些已连接的选项卡。
Buttons are places in blade template: 按钮是blade模板中的位置:

<button @click="openModal('login')">
<button @click="openModal('register')">

//Vue component also is placed in blade template
<auth-component :show="showModal" :active_tab="activeTab" @close="showModal = false"></auth-component>

/resources/assets/js/app.js : /resources/assets/js/app.js
I want to pass openModal() parameter to a component... 我想将openModal() 参数传递给组件...

const app = new Vue({
    el: '#app',
    data: {
        showModal: false,
        activeTab: '',
    },
    methods: {
        openModal(tab) {
            this.activeTab = tab;
            this.showModal = true;
        }
    },
});

... and use that parameter as Vue prop to set certain bootstrap-tab as active inside Vue-component: ...并将该参数用作Vue prop以将某些bootstrap-tab设置为在Vue组件内部处于活动状态:

AuthComponent.vue : AuthComponent.vue

<div class="modal-mask" @click="close" v-show="show">
    <div class="modal-wrapper">
        <div class="modal-container" @click.stop>
            <b-card no-body>
                <b-tabs card>
                    <b-tab title="Login" :active="active_tab === login">
                    <b-tab title="Register" :active="active_tab === register">
                </b-tabs>
            </b-card>
        </div>
    </div>
</div>
...
props: ['show', 'active_tab'],

But my current code does not work. 但是我当前的代码不起作用。 There are no console errors. 没有控制台错误。 Just first tab (login) is always active after modal is opened. 打开模态后,仅第一个选项卡(登录)始终处于活动状态。


UPDATE : I moved to 更新 :我搬到了

<b-tab title="Login" :active="active_tab === login">
<b-tab title="Register" :active="active_tab === register">

and I see that in Vue console needed tab get active: true property. 并且我看到在Vue控制台中,需要的选项卡被active: true属性。 But while active: true Register tab is anyway display: none . 但是在active: true仍然display: none注册”选项卡display: none So I guess setting active property as true isn't enough to actually make tab content visible. 因此,我想将active属性设置为true不足以实际使选项卡内容可见。

JSFiddle component code JSFiddle组件代码

You could try wrapping them in <b-tabs v-model="tabIndex"></b-tabs> and then have a computed method that determines the index based off the prop. 您可以尝试将它们包装在<b-tabs v-model="tabIndex"></b-tabs> ,然后使用一种计算出的方法来根据道具确定索引。

EDIT 编辑

computed: {
  tabIndex: {
    get: function() {
      if (this.activeTab === 'register') {
        return 1;
      } else {
        return 0;
      }
    },
    set: function(newValue) {
      return newValue;
    }
  }
}

Try passing openModal.bind(undefined,'login') so you return a function with the bound params instead of invoking that function. 尝试传递openModal.bind(undefined,'login')以便您返回具有绑定参数的函数,而不是调用该函数。

Also, the tabs component is set with the v-model attribute. 另外,选项卡组件使用v-model属性设置。 It uses numeric indexes to set the active tab. 它使用数字索引来设置活动选项卡。

Maybe make a lookup object to simplify things: const tab_map = { login: 0, register: 1 } 也许做一个查找对象来简化事情: const tab_map = { login: 0, register: 1 }

Then you can setup openModal.bind(undefined, tab_map.login) 然后您可以设置openModal.bind(undefined, tab_map.login)

For the v-model setup on tabs, check this example out: https://bootstrap-vue.js.org/docs/components/tabs/#external-controls 对于选项卡上的v模型设置,请查看以下示例: https : //bootstrap-vue.js.org/docs/components/tabs/#external-controls

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

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