前端技能包

ES6


<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><script>// 變量定義var a=1;let b=5;  // 現在使用let 定義變量// 對象解構let person={"name":"dc","age":25}console.log(person.name); // 原操作:對象.屬性名console.log(person.age);let {name,age}=person; // 對象解構console.log(name);console.log(age);// 模板字符串let info=`你好,${name}。今年${age}`// promisefunction get(url) {return new Promise((resolve, reject) => {$.ajax({url: url,type: "GET",success(result) {resolve(result);},error(error) {reject(error);}});});}// Async函數async function func1() {// 業務、計算let x = 101;if (x % 2 === 0) {return x;} else {throw new Error("x不是偶數");}}func1().then(data => console.log("then", data)).catch(err => console.log("err", err)); // 調用func1// await + asyncasync function func2() {let x = await func1(); // await同步等待func1() 結果后 結束console.log("x", x);}func2();// ES6模塊化// user.jsconst user = {username: "張三",age: 18}function isAdult(age){if (age > 18) {console.log('成年人');} else {console.log('未成年');}}// main.js// exportexport { user, isAdult }import { user, isAdult } from './lib/user.js';isAdult(user.age);</script>
</body>
</html>

npm 包管理工具

在這里插入圖片描述

Vue3

在這里插入圖片描述


$ npm create viteNeed to install the following packages:create-vite
Ok to proceed? (y) y? Project name: · vite-project
? Select a framework: · vue
? Select a variant: · vueScaffolding project in ./vite-project...Done. Now run:cd vite-projectnpm installnpm run dev

cd vite-project
npm install
npm run dev

在這里插入圖片描述

插值


<script setup>
let name = "張三"let car = {brand: "小米",price: 999
}
</script><template><!-- {{val}}  插值表達式,頁面任何位置取值--><h2>姓名:{{name}}</h2><h2>品牌:{{car.brand}}</h2><h2>價格:{{car.price}}</h2>
</template><style scoped>
</style>

常用指令

v-html

// 2. 指令
let msg = "<p style='color: red'>你好</p>"<template><h2>姓名:{{name}}</h2><h2>品牌:{{car.brand}}</h2><h2>價格:{{car.price}}</h2><div v-html="msg"></div>
</template>

在這里插入圖片描述


<script setup>//  指令: v-xxx;
// 基礎指令: v-html、v-text
// 事件指令: v-onlet msg = "<p style='color: red'>你好</p>";function buy() {alert("購買成功");
}</script><template><h2>姓名:{{name}}</h2><h2>品牌:{{car.brand}}</h2><h2>價格:{{car.price}}</h2><button @click="buy">購買</button><div v-html="msg"></div><div>{{msg}}</div><div v-text="msg"></div></template>

在這里插入圖片描述

v-if 、v-for

<span style="color: green" v-if="car.price < 1000"> 便宜</span>
<span style="color: red" v-if="car.price >= 1000"> 太貴</span>
<li v-for="(f, i) in fruits">{{ f }} ==> {{ i }}</li>

v-bind


<script>
// 3. 屬性綁定:v-bind
let url = "https://www.baidu.com";
</script><template><a v-bind:href="url">Go !</a>
</template>

響應式變化:數據的變化 可以更新到頁面效果上 ref()、reactive()


<script>// 3. 屬性綁定:v-bind;默認數據 不具備響應式特性let url = ref("https://www.example.com");// 響應式特性:數據的變化 可以更新到頁面效果上function changeUrl() {console.log(url);// 改變的時候需要 url.valueurl.value = "https://www.atguigu.com";}// ref():// 1. 把基本數據使用 ref() 包裝成響應式數據// 2. 使用 代理對象.value = ""// 3. 頁面取值、屬性綁定 直接 {{url}}// 傳遞對象reactive()const money = reactive({money: 1000,name: "parent",});</script><template><a :href="url" :abc="url">Go ! {{ url }}</a><button @click="changeUrl">改變地址</button>
</template>

表單綁定 v-model :雙向綁定


<script setup>
import { reactive } from "vue";const data = reactive({username: "zhangsan",agree: true,hobby: [],gender: "女",degree: "",course: []
})
</script><template><p style="background-color: azure"><label>姓名(文本框):</label><input v-model="data.username"/></p>
</template>

計算屬性

<script setup>import { ref, computed } from 'vue';// 假設 car 和 num 是已經定義的響應式對象或引用const car = {price: 10000 // 示例價格};const num = ref({ value: 1 }); // 示例數量// 計算總價const totalPrice = computed(() => car.price * num.value);</script><template><!-- <button v-on:click="buy">購買</button> --><button @click.once="buy">購買 {{ totalPrice }}</button>
</template>

監聽 watch()


<script>const num = ref({ value: 1 });// 監聽: watch/watchEffect// num數字發生變化時,開啟回調函數watch(num, (value, oldValue, onCleanup) => {console.log("value", value);console.log("oldValue", oldValue);if (num.value > 3) {alert("超出限購數量");num.value = 3;}});</script>

<script>// 監聽一堆事件watchEffect(() => {if (num.value > 3) {alert("超出限購數量");num.value = 3;}if (car.price > 11000) {alert("太貴了");}});</script>

生命周期 mounted()


<script setup>import { ref, onMounted } from 'vue';// 定義一個響應式變量 countconst count = ref(0);// 假設 elementId 是一個已定義的元素 IDconst elementId = "ds";// onMounted 生命周期鉤子onMounted(() => {console.log("掛載完成!!!")});
</script>

組件傳值

父傳子 v-bind

Son


<script setup>
// 1、定義屬性
let props = defineProps( ['money'] );</script><template><div style="background-color: #646cff; color: white;"><h3>Son</h3><!--  只讀值 read only--><div>賬戶:{{ props.money }}</div></div>
</template><style scoped>
</style>

Father


<script setup>
import Son from "./Son.vue";
import { ref } from "vue";// 1、父傳子,單向數據流,子變了 父親的不會變
const money = ref(100);</script><template><div style="background-color: #f9f9f9"><h2>Father</h2><!--  屬性綁定傳遞值--><Son :money="money" /></div>
</template><style scoped>
</style>
let props = defineProps({money: {type: Number,required: true,default: 200},books: Array
});

子傳父 emit


<script setup>
import Son from "./Son.vue";// 1、父傳子
const data = reactive({money: 1000,name: "parent",
});function moneyMinis(arg) {// alert("感知到兒子買了棒棒糖" + arg)data.money += arg;
}</script><template><div style="background-color: #f9f9f9"><h2>Father</h2><Son :money="data.money" @buy="moneyMinis"/><!-- <Son v-bind="data"/> --></div>
</template><style scoped>
</style>

<script setup>// 1、定義屬性
let props = defineProps( ['money'] );
// 2、使用 emit: 定義事件
const emits = defineEmits(['buy']);function buy() {// props.money -= 5; // 這里不直接修改 props,而是通過 emit 通知父組件emits('buy', -5);
}
</script><template><div style="background-color: #646cff; color: white"><h3>Son</h3><div>賬戶:{{ props.money }}</div><button @click="buy">買棒棒糖</button></div>
</template><style scoped>
</style>

插槽

Father

<script setup>
</script><template><div style="background-color: #f9f9f9"><h2>Father</h2><Son ><template v-slot:title>哈哈SonSon</template></Son></div>
</template><style scoped>
</style>

Son


<script setup>
</script><template><div style="background-color: #646cff; color: white"><h3><slot name="title">哈哈Son</slot></h3><button @click="buy"><slot name="btn"/></button></div>
</template><style scoped>
</style>

Vue - Router

npm install vue-router

在這里插入圖片描述

在 src/router/index.js 中配置路由:

index.js


// 引入必要的模塊
import { createRouter, createWebHashHistory } from 'vue-router';
import Home from '../views/Home.vue'; // 假設 Home 組件位于 views 文件夾下
import Hello from '../views/Hello.vue'; // 假設 Hello 組件位于 views 文件夾下// 定義路由規則
const routes = [{ path: '/', component: Home },{ path: '/hello', component: Hello },{path: '/haha',component: () => import('../views/Haha.vue') // 動態導入 Haha 組件}
];// 創建路由器
const router = createRouter({history: createWebHashHistory(), // 使用 hash 模式routes // 路由規則
});// 導出路由器
export default router;

在 src/main.js 中使用路由器:

main.js

import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router'let app = createApp(App);// 1、使用 router
app.use(router)
app.mount('#app');

在 App.vue 中使用 和


<template><router-link to="/">首頁</router-link><router-link to="/hello">Hello</router-link><router-link to="/haha">Haha</router-link><!-- ... --><hr /><router-view></router-view><!-- ... --><!-- 1、整合 vue-router --><!-- 2、配置 vue-router --><!--     配置路由表 --><!--     創建路由器 --><!-- 3、Vue 實例使用 router --><!-- 4、配置 router-link 和 router-view -->
</template>

Axios

發送請求

App.vue


<script setup>
import axios from 'axios'function getInfo() {axios.get("http://43.139.239.29/get").then(resp => {console.log(resp.data);// config: 請求配置// data: 服務器的響應數據   √√√// headers: 響應頭// request: 請求對象// status: 響應狀態碼// statusText: 響應描述文本})
}function getInfoParam() {axios.get("http://43.139.239.29/get", {params: {id: 1,username: 'zhangsan'}}).then(resp => {console.log(resp);});
}function postInfoParam() {// 數據會被自動轉為jsonaxios.post("http://43.139.239.29/post",{id: 222,username: 'zhangsan',age: 18}).then(resp => {console.log(resp);});
}</script><template><button @click="getInfo">GET請求</button><button @click="getInfoParam">GET請求 帶參數</button><button @click="postInfoParam">POST 請求</button>
</template><style scoped>
</style>

實例配置

index.js

import axios from 'axios';const http = axios.create({baseURL: 'http://43.139.239.29',timeout: 1000,headers: { 'X-Custom-Header': 'foobar' }
});export default http;

App.vue


<script setup>
import http from './index'function getInfo() {http.get("/get").then(resp => {console.log(resp.data);// config: 請求配置// data: 服務器的響應數據   √√√// headers: 響應頭// request: 請求對象// status: 響應狀態碼// statusText: 響應描述文本})
}</script><template><button @click="getInfo">GET請求</button>
</template><style scoped>
</style>

攔截器

index.js


import axios from 'axios';// 創建自定義 Axios 實例
const http = axios.create({baseURL: 'http://43.139.239.29',timeout: 1000,headers: { 'X-Custom-Header': 'foobar' }
});// 添加請求攔截器
http.interceptors.request.use(function (config) {// 在發送請求之前做些什么return config;},function (error) {// 對請求錯誤做些什么console.log("請求錯誤", error);return Promise.reject(error);}
);// 添加響應攔截器
http.interceptors.response.use(function (response) {// 2xx 范圍內的狀態碼都會觸發該函數。// 對響應數據做點什么// 返回響應數據主體內容return response.data;},function (error) {// 超出 2xx 范圍的狀態碼都會觸發該函數。// 對響應錯誤做點什么console.log("響應錯誤", error);ElMessage.error("服務器錯誤" + error.message); // 使用 Element UI 的 Message 組件顯示錯誤消息return Promise.reject(error);}
);// 導出 http 實例
export default http;

在這里插入圖片描述

Pinia

類似后端的Redis

在這里插入圖片描述

npm install pinia

money.js

import { defineStore } from 'pinia';// 定義一個 money存儲單元
export const useMoneyStore = defineStore('money', {state: () => ({ money: 100 }),getters: {rmb: (state) => state.money,usd: (state) => state.money * 0.14,eur: (state) => state.money * 0.13,},actions: {win(arg) {this.money += arg;},pay(arg) {this.money -= arg;}},
});

Wallet.vue


<script setup>
import { useMoneyStore } from './money.js'
let moneyStore = useMoneyStore();
</script><template><div><h2>¥: {{moneyStore.rmb}}</h2><h2>$: {{moneyStore.usd}}</h2><h2>€: {{moneyStore.rmb}}</h2></div>
</template><style scoped>
div {background-color: #f9f9f9;
}
</style>

game.vue


<script setup>
import { useMoneyStore } from './money.js';let moneyStore = useMoneyStore();function guaguale() {moneyStore.win(100);
}function bangbang() {moneyStore.pay(5);
}
</script><template><button @click="guaguale">刮刮樂</button><button @click="bangbang">買棒棒糖</button>
</template><style scoped>
</style>

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

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

相關文章

大數據(1) 大數據概述

一、大數據時代 1.三次信息化浪潮 二、什么是大數據 1.四個特點 4V&#xff1a;數據量&#xff08;Volume&#xff09;大、數據類型&#xff08;Variety&#xff09;繁多、處理速度&#xff08;Velocity&#xff09;快、價值密度&#xff08;Value&#xff09;低 三、大數據…

element-plus 單選組件 el-radio,選不上,又沒報錯,直接復制官網也不行解決方案

在使用 Vue 框架開發項目時&#xff0c;Element UI 是常用的組件庫。最近在開發中遇到了 Element 單選框組件el-radio的雙向綁定問題&#xff0c;直接復制element官網上的的案例下來也是不得&#xff0c;經過調試和探索&#xff0c;終于找到了解決方案&#xff0c;特此記錄分享…

使用 Amazon Q Developer CLI 快速搭建各種場景的 Flink 數據同步管道

在 AI 和大數據時代&#xff0c;企業通常需要構建各種數據同步管道。例如&#xff0c;實時數倉實現從數據庫到數據倉庫或者數據湖的實時復制&#xff0c;為業務部門和決策團隊分析提供數據結果和見解&#xff1b;再比如&#xff0c;NoSQL 游戲玩家數據&#xff0c;需要轉換為 S…

開疆智能Ethernet/IP轉Modbus網關連接質量流量計配置案例

首先設置modbus從站的485參數&#xff0c;確保網關和從站的485參數保持一致。 設置完成后打開網關配置軟件并新建項目 先設置網關在Ethernet一側的IP地址以及數據轉換長度。 設置網關的Modbus參數如波特率9600無校驗8數據位&#xff08;無校驗選8&#xff0c;有校驗選9&#xf…

多智能體MPE環境遇到的若干問題

最近學習MADDPG算法&#xff0c;用MPE環境來測試算法性能。于是便下載了pettingzoo包&#xff0c;運行了simple_tag_v3環境&#xff0c;此環境中有獵人、逃亡者和障礙物。 問題1: MPE中的simple_tag_v3環境&#xff0c;在渲染時看似移動的問題 由于相機視角跟隨導致的視覺錯覺…

[特殊字符] FFmpeg 學習筆記

一、FFmpeg 簡介 FFmpeg 是一個開源跨平臺的視頻和音頻處理工具&#xff0c;支持錄制、轉換、流處理等功能。 官網&#xff1a;https://ffmpeg.org 安裝命令&#xff08;macOS&#xff09;&#xff1a; brew install ffmpeg二、基本命令結構 ffmpeg -i 輸入文件 [參數] 輸出…

leetcode Top100 238. 除自身以外數組的乘積|數組系列

題目鏈接&#xff1a;238. 除自身以外數組的乘積 - 力扣&#xff08;LeetCode&#xff09; 238. 除自身以外數組的乘積|數組系列 給你一個整數數組 nums&#xff0c;返回 數組 answer &#xff0c;其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘積 。 題目數據 …

【Liunx專欄_6】Linux線程概念與控制

目錄 1、線程是什么&#xff1f;通過一個圖來理解……2、Linux進程和線程&#xff1f;2.1、之間的關系和區別2.2、線程的優缺點&#xff1f; 3、線程的創建3.1、POSIX線程庫3.2、創建線程3.3、PS查看運行的線程 4、線程的終止5、線程的等待6、線程分離7、線程封裝 1、線程是什么…

「Java基本語法」標識符、關鍵字與常量

知識點解析 1&#xff0e;標識符&#xff08;Identifiers&#xff09;&#xff1a;用于命名類、方法、變量等。 標識符命名規則&#xff1a; 標識符由字母&#xff08;A-Z&#xff0c;a-z&#xff09;、數字&#xff08;0-9&#xff09;、下劃線“_”或美元符號“$”組成。標…

Nginx Stream 層連接數限流實戰ngx_stream_limit_conn_module

1.為什么需要連接數限流&#xff1f; 數據庫/Redis/MQ 連接耗資源&#xff1a;惡意腳本或誤配可能瞬間占滿連接池&#xff0c;拖垮后端。防御慢速攻擊&#xff1a;層疊式限速&#xff08;連接數&#xff0b;帶寬&#xff09;可阻擋「Slow Loris」之類的 TCP 低速洪水。公平接入…

LLMs之Structured Output:vLLM 結構化輸出指南—從約束生成到自動解析與高效實現

LLMs之Structured Output&#xff1a;vLLM 結構化輸出指南—從約束生成到自動解析與高效實現 導讀&#xff1a;隨著大語言模型&#xff08;LLM&#xff09;在各類任務中的廣泛應用&#xff0c;如何使其輸出具備可控性、結構化與可解析性&#xff0c;成為實際部署中的關鍵問題。…

32 C 語言字符處理函數詳解:isalnum、isalpha、iscntrl、isprint、isgraph、ispunct、isspace

1 isalnum() 函數 1.1 函數原型 #include <ctype.h>int isalnum(int c); 1.2 功能說明 isalnum() 函數用于檢查傳入的整數參數是否為 ASCII 編碼的字母或數字字符&#xff08;A - Z、a - z、0 - 9&#xff0c;對應 ASCII 值 65 - 90、97 - 122、48 - 57&#xff09;。…

在網絡排錯中,經常會用到的操作命令和其作用

在網絡排錯中&#xff0c;經常會用到的操作命令和其作用 網絡排錯是確保網絡連接正常運行的重要環節&#xff0c;通過使用一系列工具和命令&#xff0c;可以有效診斷和解決網絡問題。以下是常用的網絡排錯命令及其作用&#xff1a; 1.ping ping 是一個用于測試主機之間連通性…

C++中友元(friend)高級應用和使用示例

下面列出幾個 高級友元應用場景 與典型設計模式&#xff0c;并配以示例&#xff0c;幫助大家在實際項目中靈活運用 friend 機制。 1. ADL 友元注入&#xff08;“注入式友元”&#xff09; 場景&#xff1a;為某個類型定義非成員操作符&#xff08;如算術、流插入等&#xff0…

TCP相關問題 第一篇

TCP相關問題1 1.TCP主動斷開連接方為什么需要等待2MSL 如上圖所示:在被動鏈接方調用close&#xff0c;發送FIN時進入LAST_ACK狀態&#xff0c;但未收到主動連接方的ack確認&#xff0c;需要被動連接方重新發送一個FIN&#xff0c;而為什么是2MSL&#xff0c;一般認為丟失ack在…

STM32啟動文件學習(startup_stm32f40xx.s)

原代碼 ;******************** (C) COPYRIGHT 2016 STMicroelectronics ******************** ;* File Name : startup_stm32f40xx.s ;* Author : MCD Application Team ;* version : V1.8.0 ;* date : 09-November-2016 ;* Desc…

uni-app學習筆記二十三--交互反饋showToast用法

showToast部分文檔位于uniapp官網-->API-->界面&#xff1a;uni.showToast(OBJECT) | uni-app官網 uni.showToast(OBJECT) 用于顯示消息提示框 OBJECT參數說明 參數類型必填說明平臺差異說明titleString是提示的內容&#xff0c;長度與 icon 取值有關。iconString否圖…

【Ragflow】26.RagflowPlus(v0.4.0):完善解析邏輯/文檔撰寫模式全新升級

概述 在歷經半個月的間歇性開發后&#xff0c;RagflowPlus再次迎來一輪升級&#xff0c;正式發布v0.4.0。 開源地址&#xff1a;https://github.com/zstar1003/ragflow-plus 更新方法 下載倉庫最新代碼&#xff1a; git clone https://github.com/zstar1003/ragflow-plus.…

【論文解讀】Toolformer: 語言模型自學使用工具

1st author: ?Timo Schick? - ?Google Scholar? paper: Toolformer: Language Models Can Teach Themselves to Use Tools | OpenReview NeurIPS 2023 oral code: lucidrains/toolformer-pytorch: Implementation of Toolformer, Language Models That Can Use Tools, by…

Spring 官方推薦構造函數注入

1. 依賴關系明確 構造函數注入可以清晰地聲明類的依賴關系&#xff0c;所有必需的依賴項都通過構造函數參數傳遞&#xff0c;使得代碼的可讀性更高。這種方式讓類的使用者能夠直觀地了解類的依賴&#xff0c;而不需要通過注解或反射來猜測。 2. 增強代碼健壯性 構造函數注入…