ホームページ  >  記事  >  ウェブフロントエンド  >  VUE ページのサウンド + タイトル フラッシュ通知コンポーネントを共有する

VUE ページのサウンド + タイトル フラッシュ通知コンポーネントを共有する

藏色散人
藏色散人転載
2023-03-14 17:28:161413ブラウズ

この記事は、VUE に関する関連知識を提供します。主に、VUE ページのサウンドとタイトル フラッシュ通知のコンポーネントを共有します。興味のある友人は、それを見てください。すべての人に役立つことを願っています。

VUE ページのサウンド + タイトル フラッシュ通知コンポーネントを共有する

#VUE ページサウンドタイトル点滅通知コンポーネント

1. 使用方法

1.1 コンポーネントテンプレートリファレンス

<PageNotice ref="pageNotice" sound="/xxx/new_message.mp3" />

1.2 サポートされているパラメータ

sound: 通知中に再生されるサウンド

##1.3 動的呼び出しメソッド

$refs.pageNotice.tip(&#39;你好&#39;,&#39;新消息&#39;) $refs.pageNotice.tip(&#39;有新客户访问&#39;)

2、コンポーネントのソースコード

# Page Notice コンポーネントのソース コードは次のとおりです。

<template>
    <div>
        <audio ref="audio" :src="sound"></audio>
    </div>
</template>
<script>
export default {
    name: "PageNotice",
    props: {
        sound: {
            type: String,
            default: &#39;&#39;
        },
    },
    data() {
        return {
            tipTimer: null,
            tipTimerCount: 0,
            titleOld: null,
        }
    },
    methods: {
        tip(msg, type) {
            this.doPageTitle(msg, type)
            if (this.sound) {
                this.doPlaySound()
            }
        },
        doClearTimer() {
            clearInterval(this.tipTimer)
            this.tipTimer = null
            if (this.titleOld) {
                window.document.title = this.titleOld
            }
            this.tipTimerCount = 0
        },
        doPageTitle(msg, type) {
            type = type || &#39;提醒&#39;
            if (this.tipTimer) {
                this.doClearTimer()
            }
            this.titleOld = document.title
            this.tipTimerCount = 0
            this.tipTimer = setInterval(() => {
                this.tipTimerCount++
                if (this.tipTimerCount % 2 === 0) {
                    window.document.title = &#39;【&#39; + type + &#39;】&#39; + msg
                } else {
                    window.document.title = &#39;&#39; + msg
                }
                if (this.tipTimerCount > 6) {
                    this.doClearTimer()
                }
            }, 500)
        },
        doPlaySound() {
            let audio = this.$refs.audio
            if (!audio) {
                return
            }
            try {
                audio.pause()
                audio.play()
            } catch (e) {
            }
        }
    }
}
</script>

推奨される学習: "

vue.js ビデオ チュートリアル

"

以上がVUE ページのサウンド + タイトル フラッシュ通知コンポーネントを共有するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はlearnku.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。