vue 经典案例

1、走马灯

<body>
<div id='app'>
    <button @click="start">浪起来</button>
    <button @click="stop">低调</button>
    <h3>{{title}}</h3>
</div>
</body>
<script>
    const vm = new Vue({
        el:"#app",
        data:{
            title:"不要浪,别团",
            Interva: null
        },
        methods: {
            start(){
                clearInterval(this.Interva)
                console.log(this.title.substr(0,1));
                console.log(this.title.substring(0, 1));
                this.Interva = setInterval(()=>{
                    this.title = this.title.substr(1) + this.title.substring(0,1);
                },300)
            },
            stop(){
                clearInterval(this.Interva);
            }
        }
    })
</script>

2、简单计算器

<body>
    <div id="app">
        <input type="text" v-model='num1'>
        <select v-model='opration'>
            <option value="+">+</option>
            <option value="-" selected>-</option>
            <option value="*">*</option>
            <option value="/">/</option>
        </select>
        <input type="text" v-model="num2">
        <button @click="calc">=</button>
        <span style="background-color: aqua;">{{sum}}</span>
    </div>
</body>
<script>
    const vm = new Vue({
        el: '#app',
        data: {
            num1: 0,
            opration: "-",
            num2: 0,
            sum: 0
        },
        methods: {
            calc() {
                this.num1 = Number(this.num1);
                switch (this.opration) {
                    case "+":
                        this.sum = this.num1 + this.num2;
                        break;
                    case "-":
                        this.sum = this.num1 - this.num2;
                        break;
                    case "*":
                        this.sum = this.num1 * this.num2;
                        break;
                    case "/":
                        this.sum = this.num1 / this.num2;
                        break;
                }
            }
        }
    })
</script>

3、循环渲染下列数据,展示数据的名字。点击这--行能够alert出数据的名字。顶部有搜索框,后面有个搜索按钮
点击搜索按钮可以按照输入的内容检索列表。

    <div id="app">
        {{title}}
        <ul>
            <!-- i为索引 item为内容 -->
            <input type="text" v-model="item1">
            <button @click="search">检索</button>
            <li v-for="(item,i) in list" @click="show(item.name)" v-show="flag">id:{{item.id}} ----值:{{item.name}}</li>

        </ul>
        <ul>
            <li v-for="(item,i) in list1">id:{{item.id}} ----值:{{item.name}} </li>
        </ul>
    </div>
</body>
<script>
    const vm = new Vue({
        el: "#app",
        data: {
            list: [{ id: 3, name: "张三丰" },
            { id: 5, name: "张无忌" },
            { id: 13, name: "杨逍" },
            { id: 33, name: "殷天正" },
            { id: 12, name: "赵敏" },
            { id: 97, name: "周芷若" }],
            list1: [],
            item1: null,
            flag:true
        },
        methods: {
            show(name) {
                alert(name);
            },
            search() {
                this.list1 = this.list.filter(item => {
                    return item.name == this.item1;
                })
                if(this.list1.length==0){
                    this.flag = true;
                }else{
                    this.flag = false;
                }
            }
        }
    })
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
作为AI助手,我需要先声明一下:WebRTC技术是一种实时通信的技术,用于浏览器之间的音视频通话、屏幕共享等场景。而Vue是一种前端框架,用于构建用户界面。 因此,WebRTC和Vue并不是一种直接相关的技术,但是可以通过Vue来构建WebRTC应用的用户界面。 下面是一个简单的WebRTC Vue案例: 1. 安装Vue和WebRTC相关的库 ``` npm install vue webrtc-adapter vue-webrtc ``` 2. 在Vue组件中引入WebRTC组件 ```html <template> <div> <video ref="localVideo" autoplay muted></video> <video ref="remoteVideo" autoplay></video> <button @click="startCall">Start Call</button> <button @click="endCall">End Call</button> </div> </template> <script> import VueWebRTC from 'vue-webrtc' export default { name: 'WebRTCComponent', components: { VueWebRTC }, data () { return { localStream: null, remoteStream: null, isCalling: false } }, methods: { async startCall () { this.isCalling = true this.localStream = await navigator.mediaDevices.getUserMedia({ audio: true, video: true }) this.$refs.localVideo.srcObject = this.localStream const rtcPeerConnection = new RTCPeerConnection({ iceServers: [ { urls: 'stun:stun.stunprotocol.org' } ] }) this.localStream.getTracks().forEach(track => rtcPeerConnection.addTrack(track, this.localStream)) rtcPeerConnection.addEventListener('icecandidate', event => { if (event.candidate) { // send ice candidate to remote peer } }) rtcPeerConnection.addEventListener('track', event => { this.remoteStream = event.streams[0] this.$refs.remoteVideo.srcObject = this.remoteStream }) // create offer and set local description const offer = await rtcPeerConnection.createOffer() await rtcPeerConnection.setLocalDescription(offer) // send offer to remote peer }, endCall () { // close peer connection and release stream resources this.isCalling = false this.localStream.getTracks().forEach(track => track.stop()) this.remoteStream.getTracks().forEach(track => track.stop()) const rtcPeerConnection = this.$refs.vueWebRTC.rtcPeerConnection if (rtcPeerConnection) { rtcPeerConnection.close() } } } } </script> ``` 3. 在Vue实例中使用WebRTC组件 ```js import Vue from 'vue' import WebRTCComponent from './WebRTCComponent.vue' new Vue({ el: '#app', components: { WebRTCComponent }, template: '<WebRTCComponent/>' }) ``` 在这个案例中,我们使用了vue-webrtc组件来简化WebRTC的使用。具体来说,我们在startCall方法中使用getUserMedia获取本地音视频流,创建RTCPeerConnection实例,并将本地流添加到peer connection中。然后,我们使用createOffer方法创建一个offer并将其设置为本地的SDP(Session Description Protocol)。最后,我们将offer发送给远程peer,并等待远程peer的answer。在answer到达之后,我们将其设置为远程SDP,并完成peer connection的建立。 需要注意的是,在实际应用中,我们需要处理各种事件,例如网络中断、peer connection失败等等。同时,我们还需要考虑如何安全地传输音视频流以及如何支持多人通话等场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值