【问题标题】:How to call a vue.js function on page load如何在页面加载时调用 vue.js 函数
【发布时间】:2017-04-04 12:03:55
【问题描述】:

我有一个帮助过滤数据的功能。当用户更改选择时,我正在使用v-on:change,但我还需要在用户选择数据之前调用该函数。我之前使用ng-initAngularJS 做了同样的事情,但我知道vue.js 中没有这样的指令

这是我的功能:

getUnits: function () {
        var input = {block: this.block, floor: this.floor, unit_type: this.unit_type, status: this.status};

        this.$http.post('/admin/units', input).then(function (response) {
            console.log(response.data);
            this.units = response.data;
        }, function (response) {
            console.log(response)
        });
    }

blade 文件中,我使用刀片表单来执行过滤器:

<div class="large-2 columns">
        {!! Form::select('floor', $floors,null, ['class'=>'form-control', 'placeholder'=>'All Floors', 'v-model'=>'floor', 'v-on:change'=>'getUnits()' ]) !!}
    </div>
    <div class="large-3 columns">
        {!! Form::select('unit_type', $unit_types,null, ['class'=>'form-control', 'placeholder'=>'All Unit Types', 'v-model'=>'unit_type', 'v-on:change'=>'getUnits()' ]) !!}
    </div>

当我选择特定项目时,这很好用。然后,如果我点击所有让我们说all floors,它就可以工作。我需要的是当页面加载时,它调用getUnits 方法,该方法将使用空输入执行$http.post。在后端,我以某种方式处理请求,如果输入为空,它将提供所有数据。

如何在vuejs2 中执行此操作?

我的代码:http://jsfiddle.net/q83bnLrx

【问题讨论】:

    标签: javascript vue.js vuejs2


    【解决方案1】:

    您可以在 Vue 组件的 beforeMount 部分调用此函数:如下所示:

     ....
     methods:{
         getUnits: function() {...}
     },
     beforeMount(){
        this.getUnits()
     },
     ......
    

    工作小提琴:https://jsfiddle.net/q83bnLrx/1/

    Vue 提供了不同的生命周期钩子:

    我列出了几个:

    1. beforeCreate:在实例刚刚初始化之后,在数据观察和事件/观察者设置之前同步调用。
    2. created:实例创建后同步调用。在这个阶段,实例已经完成了对选项的处理,这意味着已经设置了以下内容:数据观察、计算属性、方法、监视/事件回调。但是,安装阶段尚未开始,$el 属性将不可用。
    3. beforeMount:在挂载开始前调用:即将第一次调用渲染函数。
    4. mounted:在实例刚刚挂载后调用,其中 el 被新创建的 vm.$el 替换。
    5. beforeUpdate:在数据发生变化时调用,在重新渲染和修补虚拟 DOM 之前。
    6. updated:在数据更改后调用会导致虚拟 DOM 被重新渲染和修补。

    您可以查看完整列表here

    您可以选择最适合您的钩子,并像上面提供的示例代码一样钩子来调用您的函数。

    【讨论】:

    • @PhillisPeters 你能放更多代码,或者创建一个小提琴。
    • @PhillisPeters 请查看更新后的fiddle,我已将 http post call 替换为 setTimeout 进行模拟,现在您可以看到表格中填充了数据。
    • @GeorgeAbitbol 请随时相应地更新答案。
    • 我的问题是我不知道在mounted() 部分中使用“this”,并且遇到了函数未定义错误。上面使用了“这个”,我发现这是我的问题的解决方案,但答案中没有突出显示。 OP的问题与我的相似吗?添加一个标注来解决绑定问题会使这个答案更好吗?
    【解决方案2】:

    你需要做这样的事情(如果你想在页面加载时调用该方法):

    new Vue({
        // ...
        methods:{
            getUnits: function() {...}
        },
        created: function(){
            this.getUnits()
        }
    });
    

    【讨论】:

    • 改用created
    • @PhillisPeters 你可以使用 created 或 beforeMount。
    【解决方案3】:

    您也可以使用mounted 来执行此操作

    https://vuejs.org/v2/guide/migration.html#ready-replaced

    ....
    methods:{
        getUnits: function() {...}
    },
    mounted: function(){
        this.$nextTick(this.getUnits)
    }
    ....
    

    【讨论】:

      【解决方案4】:

      请注意,当mounted 事件在组件上触发时,并非所有 Vue 组件都被替换,因此 DOM 可能还不是最终的。

      要真正模拟 DOM onload 事件,即在 DOM 准备好之后但在页面绘制之前触发,请在 mounted 内部使用 vm.$nextTick

      mounted: function () {
        this.$nextTick(function () {
          // Will be executed when the DOM is ready
        })
      }
      

      【讨论】:

        【解决方案5】:

        如果您在数组中获取数据,您可以执行以下操作。它对我有用

            <template>
            {{ id }}
            </template>
            <script>
        
            import axios from "axios";
        
                export default {
                    name: 'HelloWorld',
                    data () {
                        return {
                            id: "",
        
                        }
                    },
            mounted() {
                        axios({ method: "GET", "url": "https://localhost:42/api/getdata" }).then(result => {
                            console.log(result.data[0].LoginId);
                            this.id = result.data[0].LoginId;
                        }, error => {
                            console.error(error);
                        });
                    },
        </script>
        

        【讨论】:

          猜你喜欢
          • 2016-09-03
          • 2021-08-06
          • 2011-04-20
          • 1970-01-01
          • 2012-05-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多