【问题标题】:How to change text on the span use vue.js 2?如何使用 vue.js 2 更改跨度上的文本?
【发布时间】:2017-08-22 22:11:26
【问题描述】:

我的 vue 组件是这样的:

<template>
    <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
           aria-expanded="false" :title="...">
            ...
            <span v-if="totalNotif > 0" class="badge" id="total-notif">{{ totalNotif }}</span>
        </a>
    </li>
</template>
<script>
    ...
    export default {
        mounted() {
            this.initialMount()
        },
        ...
        computed: {
            ...mapGetters([
                'totalNotif'
            ])
        },
        methods: {
            initialMount() {
                Echo.private('App.User.' + window.Laravel.authUser.id).notification((notification) => {
                    const totalNotif = $('#total-notif').text()
                    const totalNotifNew = parseInt(totalNotif) + 1
                    $('#total-notif').text(totalNotifNew )
                })
            },
        }
    }
</script>

有效

但是,它仍然使用 jquery 来获取和更新 span 上的文本

如何使用 vue.js 2 做到这一点?

我阅读了一些参考资料,它使用watch。但我仍然对使用它感到困惑

【问题讨论】:

    标签: jquery vue.js vuejs2 vue-component


    【解决方案1】:

    改变

    const totalNotif = $('#total-notif').text()
    const totalNotifNew = parseInt(totalNotif) + 1
    $('#total-notif').text(totalAllNew)
    

    //Set this.totalNotif to 1 if not exist or equals to 0. Else increment by 1
    this.totalNotif = (this.totalNotif) ? (this.totalNotif + 1) : 1;
    

    但我看不懂代码:

        computed: {
            ...mapGetters([
                'totalNotif'
            ])
        },
    

    对于我的例子,我认为你可以:

    export default {
        mounted() {
            this.initialMount()
        },
        data() {
           return {
               totalNotif: 0
           }
        },
        ...
    

    【讨论】:

    • 这个:computed: { ...mapGetters([ 'totalNotif' ]) },。它曾经从数据库中获取全部通知。如果我按照你的步骤,total notif = 0
    • 现在的问题是如何将它与来自数据库的总通知结合起来?
    • 也许创建一个方法来调用AJAX从数据库中获取notalNotif,并在Vue的mounted()函数中调用这个方法。如果你不使用 jQuery,你可以使用 axios 进行 AJAX 调用。
    【解决方案2】:

    在模板上,您将更改跨度:

    <span v-if="totalNotif > 0" class="badge" id="total-notif" v-text="totalNotif"></span>
    

    这会将span 与值totalNotif 连接起来。 所以在 VueJs 部分,删除 jquery 部分,只更新数据 totalNotif 并自动更新 span 文本内容。

    编辑 为了更好地理解,脚本部分变为:

    export default {
        mounted() {
            this.initialMount()
        },
    
        data() {
           return {
              totalNotif: 0
           }
        },
    
        methods: {
            initialMount() {
                Echo.private('App.User.' + window.Laravel.authUser.id).notification((notification) => {
                    this.totalNotif = this.totalNotif + 1
    
                })
            },
        }
    }
    

    【讨论】:

    • 你在哪里找到“v-text”指令?
    • @g.annunziata,这个:computed: { ...mapGetters([ 'totalNotif' ]) },。它曾经从数据库中获取全部通知。如果我按照你的步骤,total notif = 0
    • @g.annunziata,现在的问题是如何将它与来自数据库的总通知结合起来?
    • 你可以阅读方法initialMount():你可以调用REST从db中获取值并直接设置this.totalNotif = dbValue
    猜你喜欢
    • 2021-12-20
    • 2020-07-24
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    相关资源
    最近更新 更多