html通过CDN引入Vue使用Vuex以及Computed、Watch监听

html通过CDN引入Vue使用Vuex以及Computed、Watch监听

近期遇到个需求,就是需要在.net MVC的项目中,对已有的项目的首页进行优化,也就是写原生html和js。但是咱是一个写前端的,写html还可以,.net的话,开发也不方便,还得安装Visual Studio启动项目。所以就计划在html文件中开发,然后移植到.net的项目中

功能说明:需要开发一个dashboard,也就是大屏可视化,多个模块,然后由图表作为主要内容。那么就意味着需要全局监听,如果筛选条件改变,那么所有的组件都需要监听到这个改变,做出相应的反应。

效果图

在这里插入图片描述

代码实现

store.js

const store = new Vuex.Store({
  state: {
      msg: 'Hello World',
      count: 5
  },
  mutations: {
      increment(state, payload) {
          console.log('的话',state,payload)
          state.count ++
      }
  },
  actions: {
      increment(context, payload) {
          setTimeout(() => {
          context.commit('increment', payload);
          }, 2000);
      }
  },
  getters: {
      msg(state) {
          return state.msg.toUpperCase();
      },
      count(state) {
          return state.count;
      }
  },
});

然后将store挂载到Vue上
主页面html


<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Vue Grid Layout 示例</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">

    <style>
        #loader-wrapper { position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; z-index: 999999; }
        #loader {
            display: block; position: relative; left: 50%; top: 40%; z-index: 1001; width: 90px; height: 90px; margin: -45px 0 0 -45px; border-radius: 50%; border: 3px solid transparent;border-top-color: #fe9501;         
            -webkit-animation: spin 2s linear infinite;
            -ms-animation: spin 2s linear infinite;
            -moz-animation: spin 2s linear infinite;
            -o-animation: spin 2s linear infinite;
            animation: spin 2s linear infinite;
        }
        #loader:before {
            content: ""; position: absolute; top: 5px; left: 5px; right: 5px; bottom: 5px; border-radius: 50%; border: 3px solid transparent; border-top-color: #fe9501;
            -webkit-animation: spin 3s linear infinite;
            -moz-animation: spin 3s linear infinite;
            -o-animation: spin 3s linear infinite;
            -ms-animation: spin 3s linear infinite;
            animation: spin 3s linear infinite;
        }
        #loader:after {
            content: ""; position: absolute; top: 15px; left: 15px; right: 15px; bottom: 15px; border-radius: 50%; border: 3px solid transparent; border-top-color: #fe9501;
            -moz-animation: spin 1.5s linear infinite;
            -o-animation: spin 1.5s linear infinite;
            -ms-animation: spin 1.5s linear infinite;
            -webkit-animation: spin 1.5s linear infinite;
            animation: spin 1.5s linear infinite;
        }
        @-webkit-keyframes spin {
            0% { -webkit-transform: rotate(0deg); -ms-transform: rotate(0deg); transform: rotate(0deg); }
            100% { -webkit-transform: rotate(360deg); -ms-transform: rotate(360deg); transform: rotate(360deg); }
        }
        @keyframes spin {
            0% { -webkit-transform: rotate(0deg); -ms-transform: rotate(0deg); transform: rotate(0deg); }
            100% { -webkit-transform: rotate(360deg); -ms-transform: rotate(360deg); transform: rotate(360deg); }
        }
        .load-text{
            color: #fe9501;
            text-align: center;
            position: relative;
            top: 55%;
            transform: translateY(-50%);
        }

        html {font-size: 100%; /*100 ÷ 16 × 100% = 625%*/}
        @media screen and (min-width:800px) and (max-width:999px) {
            html { font-size: 65%; }
        }
        @media screen and (min-width:1000px) and (max-width:1024px) {
            html { font-size: 70%; }
        }
        @media screen and (min-width:1025px) and (max-width:1280px) {
            html { font-size: 90%; }
        }
        @media screen and (min-width:1281px) and (max-width:1680px) {
            html { font-size: 100%; }
        }
        @media screen and (min-width:1681px) and (max-width:1920px) {
            html { font-size: 100%; }
        }
        @media screen and (min-width:1921px) and (max-width:2240px) {
            html { font-size: 150%; }
        }
        @media screen and (min-width:2241px){
            html { font-size: 150%; }
        }
        .vue-grid-layout {
            background: #eee;
        }

        .vue-grid-item:not(.vue-grid-placeholder) {
            background: #ccc;
            border: 1px solid black;
        }

        .vue-grid-item .resizing {
            opacity: 0.9;
        }

        .vue-grid-item .static {
            background: #cce;
        }

        .vue-grid-item .text {
            font-size: 24px;
            text-align: center;
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            margin: auto;
            height: 100%;
            width: 100%;
        }

        .vue-grid-item .no-drag {
            height: 100%;
            width: 100%;
        }

        .vue-grid-item .minMax {
            font-size: 12px;
        }

        .vue-grid-item .add {
            cursor: pointer;
        }

        .vue-draggable-handle {
            position: absolute;
            width: 20px;
            height: 20px;
            top: 0;
            left: 0;
            background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'><circle cx='5' cy='5' r='5' fill='#999999'/></svg>") no-repeat;
            background-position: bottom right;
            padding: 0 8px 8px 0;
            background-repeat: no-repeat;
            background-origin: content-box;
            box-sizing: border-box;
            cursor: pointer;
        }

        .vue-grid-item{
            display: flex;
        }

        .echart{
            width: 100%;
            height: 100%;
            min-height: 5rem;
        }

    </style>
</head>
<body>
    <div id="app">
        <!-- <grid-layout :layout="layout" :col-num="12" :row-height="30" :is-draggable="true" :is-resizable="true">
        <grid-item v-for="item in layout" :key="item.i" :x="item.x" :y="item.y" :w="item.w" :h="item.h" class="grid-item">
            {{ item.i }}
        </grid-item>
        </grid-layout> -->
        <el-button @click="cliackDialog" type="success">Button</el-button>
        <el-button @click="increment" type="success">{{count}}</el-button>
        <el-dialog :visible.sync="visible" title="Hello world">
            <p>Try Element</p>
        </el-dialog>
        <my-component title="标题" content="This is the first component"></my-component>
       



        
    </div>
    <!-- 引入Vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <!-- 引入Vuex -->
    <script src="https://unpkg.com/vuex@3.6.2/dist/vuex.js"></script>
    <!-- 引入gird组件 -->
    <script src="./vue-grid-layout.common.js"></script>
    <script src="./vue-grid-layout.umd.min.js"></script>
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/5.4.3/echarts.min.js"></script>
    <!-- 引入子组件 -->
    <script src="./son.js"></script>
    <!-- 引入vuex -->
    <script src="./store.js"></script>
    <script>
        new Vue({
            el: '#app',
            store,
            data: {
                draggable: true,
                resizable: false,
                index: 0,
                visible:false,
            },
            computed: {
                count() {
                    return this.$store.state.count;
                }
            },
            watch:{
                '$store.state.count':{
                    handler(newVal,oldVal){
                        console.log('主界面监听Vuex',newVal,oldVal)
                    }
                }
            },
            mounted(){
                this.initEchart()
            },
            methods: {
                increment() {
                    this.$store.commit('increment');
                },
                cliackDialog(){
                    this.visible = !this.visible
                },
                
            }   
        });
    </script>
</body>
</html>

子组件js

{/* <script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/5.4.3/echarts.min.js"></script> */}

var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = './son.css';
document.head.appendChild(link);

let htmlstr = `
  <div>
    <div class="son-box">{{ title }} - {{ content }} - {{str}}</div>
    <div class="echart2">2</div>
  </div>
`
// 导入 mapState 辅助函数  这样监听和通过$store监听都可以
// const { mapState } = Vuex;

// my-component.js
Vue.component('my-component', {
  props: ['title', 'content'],
  template: htmlstr,
  data(){
    return{
      str:1
    }
  },
  // computed: {
  //   ...mapState(['count'])
  // },
  watch: {
    // count:{
    //   handler(v){
    //     // 在 count 发生变化时执行的操作
    //     console.log('子组件监听Vuex', v);
    //   }
      
    // },
    '$store.state.count':{
      handler(newVal,oldVal){
          console.log('子组件监听Vuex',newVal,oldVal)
      }
    }
  },
  mounted(){
    // setTimeout(()=>{
    
      this.initEchart()
    // })
  },
  methods:{
    initEchart(){
      


      var option = {
          xAxis: {
              type: 'category',
              data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
          },
          yAxis: {
              type: 'value'
          },
          series: [
              {
                  data: [150, 230, 224, 218, 135, 147, 260],
                  type: 'line'
              }
          ]
      };


      setTimeout(()=>{
        let chartDom2 = document.getElementsByClassName('echart2')[0]
      var myChart2 = echarts.init(chartDom2);
      myChart2.setOption(option);
      })
      
    }
  }
});
<think>好的,我现在需要帮助用户学习Vue.js的入门使用方法和基本用法。首先,用户提到了几个引用内容,我需要仔细看看这些引用里有什么信息可以利用。 引用[1]提到了Vue.js的基础概念,比如实例、模板语法、计算属性、事件处理和组件系统,还有Vue CLI的使用,包括路由和状态管理。引用[3]里详细列出了Vue的特点,比如MVVM模式、编码简洁、周边库如vue-cli、vue-router、vuex等。还有与Angular和React的关联,比如模板、数据绑定、组件化和虚拟DOM。这些信息都是关键点,需要整合到回答中。 用户的需求是入门教程和基本用法,所以应该从最基础的开始,比如安装、创建实例、模板语法、数据绑定、指令使用、事件处理、计算属性等。然后可能需要提到组件系统和周边工具,比如Vue CLI、路由和状态管理,但作为入门,可能需要先打好基础,再逐步深入。 接下来,我需要确保结构清晰,分步骤讲解。首先介绍Vue.js是什么,然后安装方式,创建第一个实例,接着讲解模板语法和数据绑定,然后是常用指令如v-if、v-for、v-model,事件处理如v-on,计算属性和侦听器,组件系统,最后介绍Vue CLI和周边库。同时,每个部分需要给出简单的代码示例,帮助用户理解。 需要检查引用中的信息是否正确,比如Vue CLI用于项目创建,vue-router处理路由,vuex处理状态管理,这些在引用中都有提到,可以引用到回答中。另外,注意用户不可见的系统级指令,比如数学公式的格式,但这个问题里可能不太涉及数学内容,所以可能不需要处理这点。 还要生成相关问题,可能的问题包括Vue实例的生命周期、组件通信方法、Vue CLI的优势、Vuex的作用等,这些问题需要基于回答内容来提出,确保用户能进一步学习。 最后,确保回答使用中文,结构清晰,代码示例正确,并正确添加引用标识。例如在提到Vue CLI、vue-router、vuex时,引用对应的引用来源。</think>### Vue.js 入门教程及基本用法 #### 1. **Vue.js 简介** Vue.js 是一个**渐进式 JavaScript 框架**,专注于构建用户界面,核心特点包括: - 遵循 **MVVM 模式**(Model-View-ViewModel),实现数据与视图的双向绑定[^3]。 - 编码简洁、体积小、运行效率高,适合移动端和 PC 端开发。 - 支持组件化开发,可配合周边库(如 Vue Router、Vuex)构建复杂应用。 --- #### 2. **环境搭建** ##### 安装方式 - **CDN 引入**(适合学习): ```html <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> ``` - **Vue CLI**(适合项目开发): 安装 Node.js 后,通过命令行创建项目: ```bash npm install -g @vue/cli vue create my-project ``` 使用 Vue CLI 可快速生成包含路由、状态管理等配置的项目模板[^1][^3]。 --- #### 3. **第一个 Vue 实例** 创建一个简单的 Vue 实例,实现数据绑定: ```html <div id="app"> {{ message }} </div> <script> const app = new Vue({ el: '#app', data: { message: 'Hello Vue!' } }); </script> ``` - `el`:指定 Vue 实例挂载的 DOM 元素。 - `data`:定义响应式数据,修改 `message` 的值会实时更新视图。 --- #### 4. **模板语法与指令** Vue 使用基于 HTML 的模板语法,常用指令包括: - **文本插值**:`{{ }}` 绑定数据。 - **v-bind**:动态绑定属性,如 `v-bind:href="url"`(可简写为 `:href`)。 - **v-if / v-for**:条件渲染与列表渲染。 - **v-model**:表单输入双向绑定。 示例: ```html <div id="app"> <p v-if="showText">{{ message }}</p> <ul> <li v-for="item in list" :key="item.id">{{ item.name }}</li> </ul> <input v-model="inputText" placeholder="输入内容"> </div> ``` --- #### 5. **事件处理** 使用 `v-on` 指令监听 DOM 事件(简写为 `@`): ```html <button @click="handleClick">点击</button> <script> new Vue({ methods: { handleClick() { alert('按钮被点击!'); } } }); </script> ``` --- #### 6. **计算属性与侦听器** - **计算属性**:根据依赖数据动态计算值,结果会被缓存。 ```javascript computed: { reversedMessage() { return this.message.split('').reverse().join(''); } } ``` - **侦听器**:监听数据变化并执行逻辑。 ```javascript watch: { message(newVal, oldVal) { console.log('message 发生变化'); } } ``` --- #### 7. **组件系统** 组件是 Vue 的核心功能,支持复用和模块化开发: ```javascript // 定义组件 Vue.component('my-component', { template: '<div>{{ text }}</div>', data() { return { text: '自定义组件' }; } }); // 使用组件 <my-component></my-component> ``` --- #### 8. **Vue 周边库** - **Vue Router**:实现单页面应用(SPA)路由管理[^1]。 - **Vuex**:集中式状态管理,解决复杂组件通信问题[^3]。 - **Axios**:用于 HTTP 请求,与 Vue 配合处理数据交互。 --- #### 9. **实战步骤** 1. 通过 Vue CLI 创建项目。 2. 编写组件并配置路由(使用 Vue Router)。 3. 使用 Vuex 管理全局状态(如用户登录信息)。 4. 通过 Axios 调用 API 获取数据。 --- §§ 1. **Vue 实例的生命周期钩子有哪些?** 2. **如何实现父子组件之间的通信?** 3. **Vue CLI 相比手动配置有哪些优势?** 4. **Vuex 的核心概念(State、Mutation、Action)是什么?** --- 通过以上内容,你可以快速掌握 Vue.js 的核心用法。深入学习时,建议结合官方文档和项目实践[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_小郑有点困了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值