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);})}}
});

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/213044.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/213044.shtml
英文地址,請注明出處:http://en.pswp.cn/news/213044.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

K8S學習指南-minikube的安裝

簡介 Minikube 是一個用于在本地開發環境中運行 Kubernetes 集群的工具。它允許開發人員在單個節點上體驗 Kubernetes&#xff0c;無需配置復雜的生產環境。本指南將詳細介紹在 Windows、CentOS 和 Ubuntu 系統上安裝 Minikube 的步驟。 1. Windows 系統安裝 1.1 &#xff1…

期末速成數據庫極簡版【查詢】(3)

目錄 多表查詢 【8】多表連接——內連接 &#x1f642;等值連接 &#x1f642;自然連接 &#x1f642;非等值連接 【9】多表連接——外連接 【10】交叉連接不考 【11】聯合查詢 【12】擴展多表連接 【13】嵌套查詢 &#x1f642; 多表查詢 【8】多表連接——內連…

HIVE學習(hive基礎)

HIVE基礎介紹 一、HIVE簡介二、hive的數據類型1、基本數據類型2、復合數據類型 三、HIVE的DDL操作四、創建一個表1. 建表語句 五、修改表結構1.修改表名2. 列修改或增加3. 修改分區 五、常見函數六、一對一關聯left join左關聯right join 右關聯內連接全連接查詢只有A表的數據 …

計算機視覺-機器學習-人工智能頂會 會議地址

計算機視覺-機器學習-人工智能頂會 會議地址 最近應該要整理中文資料的參考文獻&#xff0c;很多會議文獻都需要補全會議地點&#xff08;新國標要求&#xff09;。四處百度感覺也挺麻煩的&#xff0c;而且沒有比較齊全的網站可以搜索。因此自己整理了一下計算機視覺-機器學習…

OSPF路由協議

隨著Internet技術在全球范圍的飛速發展&#xff0c;OSPF已成為目前應用最廣泛的路由協議之一。OSPF&#xff08;Open Shortest Path First&#xff09;路由協議是由IETF&#xff08;Internet Engineering Task Force&#xff09;IGP工作組提出的&#xff0c;是一種基于SPF算法的…

JS 云服務 Deno Depoly 宣布,推出定時運行功能 Deno Cron

如果需要定時執行 JS 腳本&#xff0c;以后多一個選項。 Web 構建日益復雜。編寫現代軟件包括利用云基礎設施、剖析模板代碼和管理復雜的配置&#xff0c;而開發人員只想專注于編寫業務邏輯。 Deno 旨在通過刪除配置和不必要的模板&#xff0c;從根本上簡化 Web 開發。我們將無…

網絡攻擊(三)--攻擊階段

5. 威脅建模階段 目標 了解威脅建模階段的工作內容 工作內容 威脅建模主要使用在情報搜集階段所獲取到的信息&#xff0c;來標識出目標系統上可能存在的安全漏洞與弱點。 在進行威脅建模時&#xff0c;確定最為高效的攻擊方法、所需要進一步獲取到的信息&#xff0c;以及從…

【前端】CSS浮動(學習筆記)

一、浮動 1、傳統網頁布局 網頁布局的本質&#xff1a;用 CSS 來擺放盒子&#xff0c;把盒子擺放到相應位置。 CSS 提供了三種傳統布局方式&#xff08;盒子如何進行排列順序&#xff09; 普通流&#xff08;標準流&#xff09;浮動定位 實際開發中&#xff0c;一個頁面基…

Go 反射技術判斷結構體字段數據為空

Api介紹 在Go語言中&#xff0c;反射API用于在運行時檢查類型信息、獲取和修改變量的值以及調用對象的方法。反射API包含了一組函數和類型&#xff0c;可以在程序運行時動態地操作對象。 以下是一些常用的反射API&#xff1a; reflect.TypeOf&#xff1a;返回一個值的類型信息。…

并查集基礎模板

題目我上面有人兒 代碼 #include <bits/stdtr1c.h> using namespace std; const int N 1005; int f[N]; int n; int siz[N]; // 初始化并查集 // void init() // { // for (int i 1; i < n; i) // { // f[i] i; // 初始化所有的節點都是自己的父節點 //…

Tomcat頭上有個叉叉

問題原因&#xff1a; 這是因為它就是個空的tomcat,并沒有導入項目運行 解決方案&#xff1a; war模式&#xff1a;發布模式&#xff0c;正式發布時用&#xff0c;將WEB工程以war包的形式上傳到服務器 war exploded模式&#xff1a;開發時用&#xff0c;將WEB工程的文件夾直接…

【網絡協議】LACP(Link Aggregation Control Protocol,鏈路聚合控制協議)

文章目錄 LACP名詞解釋LACP工作原理互發LACPDU報文確定主動端確定活動鏈路鏈路切換 LACP和PAgP有什么區別&#xff1f;LACP與LAG的關系LACP模式更優于手動模式LACP模式對數據傳輸更加穩定和可靠LACP模式對聚合鏈路組的故障檢測更加準確和有效 推薦閱讀 LACP名詞解釋 LACP&…

day11 前k個高頻元素

// 小頂堆 class mycomparison { public: bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) { return lhs.second > rhs.second; } }; vector<int> topKFrequent(vector<int>& nums, int k) { // 要統計元素出現…

智能外呼有什么好處?

智能外呼是一種自動化的電話營銷方式&#xff0c;利用AI智能外呼技術和大量數據分析&#xff0c;幫助企業實現與客戶之間的高效、精準、個性化的客戶溝通&#xff0c;還可以在客戶服務、市場營銷和銷售等方面帶來助力。那么&#xff0c;智能外呼有什么好處呢&#xff1f; 1. 提…

spring IOC bean為什么默認是單例的

首先解釋一下什么是單例 bean&#xff1f; 單例的意思就是說在 Spring IoC 容器中只會存在一個 bean 的實例&#xff0c;無論一次調用還是多次調用&#xff0c;始終指向的都是同一個 bean 對象 用代碼來解釋單例 bean public class UserService {public void sayHello() {Syst…

交叉編譯工具鏈makefile

linux系統默認搜索頭文件地址&#xff1a;/usr/include/文件夾&#xff1b; Windows系統默認搜索頭文件地址&#xff1a;不同軟件好像可以設置不同的地址&#xff1b;例如visual studio好像可以設置附加包含目錄&#xff0c;包含目錄等 Linux系統庫文件路徑&#xff1a;/lib文…

通過生成模擬釋放無限數據以實現機器人自動化學習

該工作推出RoboGen&#xff0c;這是一種生成機器人代理&#xff0c;可以通過生成模擬自動大規模學習各種機器人技能。 RoboGen 利用基礎模型和生成模型的最新進展。該工作不直接使用或調整這些模型來產生策略或低級動作&#xff0c;而是提倡一種生成方案&#xff0c;該方案使用…

命運天注定?

羅翔老師經常說&#xff1a;人這一生&#xff0c;能自己決定的也許只有5&#xff05;&#xff0c;有95%是你決定不了的。 不是說事在人為&#xff0c;人定勝天嗎&#xff1f; 哪吒也在電影的高潮喊出了&#xff1a;我命由我不由天。 聽上去很熱血&#xff0c;但實際咱們每個…

Java泛型:詳解使用技巧及舉例說明

Java泛型&#xff1a;詳解使用技巧及舉例說明 1. 引言 Java泛型是一項強大的編程概念&#xff0c;它允許我們編寫通用的代碼&#xff0c;在編寫代碼時不需要預先指定具體的數據類型。泛型的引入解決了在傳統的編程中需要頻繁進行類型轉換的問題&#xff0c;提高了代碼的安全性…

simulink MATLABFunction模塊中實時函數調用函數的使用

樣例 function Predyy matlabceshi(input, Time_s) input1 input; Time_s1 Time_s; Predyy ee(input1) mm(Time_s1); end 上面是主要部分&#xff0c;下面是被調用部分 function A ee(input1) A input1 * 100; end function B mm(Time_s1) B Time_s1 * 100; end 模型…