【问题标题】:How can i pass parametres from vue function in component?如何从组件中的 vue 函数传递参数?
【发布时间】:2021-12-07 21:59:01
【问题描述】:

我必须调用带有参数的函数,但我没有找到解决问题的正确方法。 NavigateTo 应该将一个字符串值传递给我的索引以切换它并导航到正确的组件(无 vue 路由器)。

Home.js

Vue.component('Home', {
    props: ['visible','logged'],
    data: function (){
        return{

        }
    },
    template: <a class="btn btn-primary display-4" on:click="NavigateTo(accedi)">Accedi</a>\n' 
    (only the button that call the function,i will post the rest of the code only if necessary)
    methods:{
        NavigateTo:function(destination){
            this.$emit('transit-inner', destination);
        }
    }
});

Index.js

<section id="Home" style="margin-top: 60px">
    <Home v-bind:visible="visible" v-bind:logged="session_info" v-on:transit-inner="goto()"></Home>
    <script>
        new Vue({
            el: '#Home',
            data:{
                visible: routes.visible_home,
                session_info: routes.status_session
            },
            methods:{
                goto: function (destination){
                    alert(destination);
                }
            }
        })
    </script>
</section>

【问题讨论】:

    标签: javascript function vue.js function-parameter


    【解决方案1】:

    您正在模板中使用内联事件处理程序 (goto()):

    <Home v-bind:visible="visible" v-bind:logged="session_info" v-on:transit-inner="goto()"></Home>
    
    

    你有两个选择:

    一、删除()

    <Home v-bind:visible="visible" v-bind:logged="session_info" v-on:transit-inner="goto"></Home>
    

    或者,如果使用 Inline Handler,则提供 $event 特殊变量

    <Home v-bind:visible="visible" v-bind:logged="session_info" v-on:transit-inner="goto($event)"></Home>
    

    【讨论】:

    • 警报仍然显示未定义
    • 已解决:我使用了第二个选项,但问题是缓存
    猜你喜欢
    • 1970-01-01
    • 2021-04-03
    • 2019-12-10
    • 1970-01-01
    • 2021-06-11
    • 2018-09-16
    • 2019-04-11
    • 1970-01-01
    • 2019-04-03
    相关资源
    最近更新 更多