監聽網絡狀態

<template>
	<div>
		
		<div class="tab">
			<div class="hd">
				<span v-bind:class="{cur:lanmu=='xinwen'}" v-on:click="change('xinwen')">新聞</span>
				<span v-bind:class="{cur:lanmu=='tupian'}" v-on:click="change('tupian')">圖片</span>
				<span v-bind:class="{cur:lanmu=='junshi'}" v-on:click="change('junshi')">軍事</span>
			</div>
			<div class="bd">
				<div class="xinwen">{{lanmu}}</div>
			</div>
		</div>		
	</div>
</template>
<script>
    export default {
		data() {
			 return {
				lanmu:"xinwen",
				onLine: navigator.onLine,
			 }
		},
		methods:{
			change:function(str){
				this.lanmu = str;	
			},
			updateOnlineStatus(e) {
				const { type } = e;
				this.onLine = type === 'online';
				console.log("網絡由異常")
			},

        },
		mounted(){
			window.addEventListener('online',  this.updateOnlineStatus);// 網絡由異常到正常時觸發

			window.addEventListener('offline', this.updateOnlineStatus);// 網絡由正常常到異常時觸發
		}
	}
</script>
<style>
	.tab{
		width: 360px;
		height: 260px;
		border:1px solid #000;
		margin:100px auto;
	}
	.tab .hd{overflow: hidden;}
	.tab .hd span{
		float: left;
		width: 120px;
		height: 40px;
		line-height: 40px;
		background-color:#ccc;
		text-align: center;
	}
	.tab .hd span.cur{
		background-color: orange;
	}
       
</style>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章