跳至主要內容
vue2使用websocket

vue代码

<template>
    <div>
        <div style="display: flex;justify-content: center;align-items: center;">
            <button @click="btnClick">发送</button>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            ws: null,
            WebSocketUrl: 'ws://localhost:7777/websocket/1',
        }
    },
    created() {
        this.handleConnect()
    },
    destroyed() {
        this.handleExit()
    },
    methods: {
        handleConnect() {
            this.ws = new WebSocket(this.WebSocketUrl)
            this.ws.onmessage = (e) => {
                console.log(e)
            }
            this.ws.onclose = () => {
                console.log('连接关闭')
            }
        },
        handleExit() {
            if (this.ws) {
                this.ws.close()
                this.ws = null
            }
        },
        btnClick() {
            this.ws.send('test')
        },
    }
}

</script>

<style scoped></style>

荒芜...小于 1 分钟前端vue2websocket
vue2中vue-echarts的使用

首先通过npm安装echarts和vue-echarts

npm i echarts vue-echarts

main.js全局引入注册

import 'echarts'
import ECharts from 'vue-echarts'

Vue.component('v-chart', ECharts)

荒芜...小于 1 分钟前端vue2echarts
vue2自动路由

每次在views下添加页面后,都需要去配置路由,总是手动去设置,会比较麻烦

于是研究了一下根据目录结构自动生成路由,这样在views下添加文件之后就会自动生成路由了

先查看需要的路由结构

const routes = [
    {
        path: '/',
        component: () => import('@/layout/index.vue'),
        children: [
            {
                path: '',
                name: 'index',
                component: () => import('@/views/index.vue')
            }, {
                path: '/test',
                name: 'test',
                component: () => import('@/views/test/index.vue')
            },{
                path: '/test/index',
                name: 'test-test',
                component: () => import('@/views/test/test.vue')
            }
        ]
    },
]

荒芜...大约 2 分钟前端vue2router