【问题标题】:vue.js reactivity and data with mixinvue.js 反应性和数据与 mixin
【发布时间】:2021-10-30 00:11:11
【问题描述】:

我正在尝试根据某些条件设置一个可以是布尔值 truefalse 的标志。

// main.js
new Vue({
    el: `#app`,
    render: h => h(App,{
      props:{
        todoID: this.dataset.id
      }
    })
  })

我的应用脚本

export default {
  name: 'App',
  props: {
    todoID: String,
  },
  data() {
    return {
      isDone: false // here is the initial flag
    }
  },
  ...

我创建了一个 mixin 方法

import Vue from "vue";

Vue.mixin({
    methods: {
        isCompleted() {
            if (myconditions){
                this.isDone = true; // I want to change the flag to true
            }
        },
    },
});

在我的模板中,如果我做{{isDone}} 我总是得到错误,我怎样才能将其更改为反应性以便可以根据条件进行更改?

这是我创建的一个演示:https://codesandbox.io/embed/vigorous-bell-p1itu?fontsize=14&hidenavigation=1&theme=dark

【问题讨论】:

  • 你能添加完整的代码吗?我想看看你的 mixin 函数调用。
  • 在模板 {{isCompleted(some variables)}} 中调用。如果我做控制台日志,我可以看到函数运行但无法更改 isDone 的值
  • 所以这意味着你的布尔值没有传递给 isCompleted 函数。
  • 好的,让我们开始吧。你能看到下面的答案。让我们试试这个看看。

标签: javascript vue.js


【解决方案1】:

Vue.mixin({
    methods: {
        isCompleted(myconditions) {
            console.log(myconditions);
            console.log(typeof myconditions);
              return  this.isDone = myconditions; // I want to change the flag to true
            
        },
    },
});
new Vue({
    el: '#vue-app',
    data: {
        isDone: false 
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

        <div id="vue-app">
            <p> result = {{ isCompleted(true) }}</p>
        </div>
    </body>

【讨论】:

  • 这并没有解决问题
  • 嘿@Rain,你能检查一下吗?可以吗?
  • 酷,别闹了,兄弟。不要忘记投票 :) 谢谢
  • 抱歉您的回复没有解决我的问题,我发的链接是我想做的
猜你喜欢
  • 2019-11-14
  • 2020-08-31
  • 2020-01-10
  • 1970-01-01
  • 2020-02-18
  • 2020-12-04
  • 2019-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多