vue3中的基本語法

目錄

基礎素材

vue3的優化

使用CompositionAPI理由

1. reactive() 函數

2. ref() 函數

2.1. ref的使用

2.2. 在 reactive 對象中訪問 ref 創建的響應式數據

3. isRef() 函數

4. toRefs() 函數

5. computed()

5.1. 通過 set()、get()方法創建一個可讀可寫的計算屬性

6. watch() 函數

6.1. 監聽用 reactive 聲明的數據源

6.2. 監聽用 ref 聲明的數據源

6.3. 同時監聽多個值

6.4. stop 停止監聽

7. LifeCycle Hooks(新的生命周期)

8. Template refs

9. vue 的全局配置

10. Suspense組件

11. vuex, router, vue 初始化寫法的變化

11.1. vue

11.2. router

11.2.1. 特點:

11.2.2. 使用

12. vuex

12.1. 使用

13. 組件注入

13.1. 父組件

13.2. 子組件

14. vue 3.x 完整組件模版結構


基礎素材

印記中文|印記中文 - 深入挖掘國外前端新領域,為中國 Web 前端開發人員提供優質文檔!

vue3官網 | 簡介 | Vue.js

pina|介紹 | Pinia 中文文檔

vue3的優化

  1. 對虛擬 DOM 進行了重寫、模板編譯速度的提升,對靜態數據的跳過處理
  2. 對數組的監控
  3. 對ts有了很好的支持
  4. 對2.x版本的完全兼容
  5. 可以有多個根節點(也有bug,比如外層開了display:flex 那么里面會收到影響,也就是說布局更靈活但也更要小心,總之請對自己與他人的代碼負責) 6. 支持Source map,雖然沒演示但是這點真的重要 7. vue2 大量的 API 掛載在 Vue 對象的原型上,難以實現 TreeShaking

使用CompositionAPI理由

  • 更好的Typescript支持
  • 在復雜功能組件中可以實現根據特性組織代碼 - 代碼內聚性。比如:排序和搜索邏輯內聚
  • 組件間代碼復用

經過了漫長的迭代,Vue 3.0 終于在上 2020-09-18 發布了,帶了翻天覆地的變化,使用了 Typescript 進行了大規模的重構,帶來了 Composition API RFC 版本,類似 React Hook 一樣的寫 Vue,可以自定義自己的 hook ,讓使用者更加的靈活。

  1. setup()
  2. ref()
  3. reactive()
  4. isRef()
  5. toRefs()
  6. computed()
  7. watch()
  8. LifeCycle Hooks(新的生命周期)
  9. Template refs
  10. globalProperties
  11. Suspense

1. reactive() 函數

reactive() 函數接收一個普通對象,返回一個響應式的數據對象,想要使用創建的響應式數據也很簡單,創建出來之后,在 setup 中 return 出去,在 template 中調用即可

<template>{{state.name}} // test
<template>
<script>
import { defineComponent, reactive, ref, toRefs } from 'vue';
export default defineComponent({setup(props, context) {let state = reactive({name: 'test'});return state}
});
</script>

2. ref() 函數

2.1. ref的使用

ref() 函數用來根據給定的值創建一個響應式的數據對象,ref() 函數調用的返回值是一個對象,這個對象上只包含一個 value 屬性,只在 setup 函數內部訪問 ref 函數需要加 .value

<template><div class="mine">{{count}} // 10</div>
</template>
<script>
import { defineComponent, ref } from 'vue';
export default defineComponent({setup() {const count = ref(10)// 在js 中獲取ref 中定義的值, 需要通過value屬性console.log(count.value);return {count}}
});
</script>

2.2. 在 reactive 對象中訪問 ref 創建的響應式數據

通過reactive 來獲取ref 的值時,不需要使用.value屬性

<template><div class="mine">{{count}} -{{t}} // 10 -100</div>
</template>
<script>
import { defineComponent, reactive, ref, toRefs } from 'vue';
export default defineComponent({setup() {const count = ref<number>(10)const obj = reactive({t: 100,count})// 通過reactive 來獲取ref 的值時,不需要使用.value屬性console.log(obj.count);return {...toRefs(obj)}}
});
</script>

3. isRef() 函數

isRef() 用來判斷某個值是否為 ref() 創建出來的對象

<script>
import { defineComponent, isRef, ref } from 'vue';
export default defineComponent({setup(props, context) {const name = 'vue'const age = ref(18)console.log(isRef(age)); // trueconsole.log(isRef(name)); // falsereturn {age,name}}
});
</script>

4. toRefs() 函數

toRefs() 函數可以將 reactive() 創建出來的響應式對象,轉換為普通的對象,只不過,這個對象上的每個屬性節點,都是 ref() 類型的響應式數據

<template><div class="mine">{{name}} // test{{age}} // 18</div>
</template>
<script>
import { defineComponent, reactive, ref, toRefs } from 'vue';
export default defineComponent({setup(props, context) {let state = reactive({name: 'test'});const age = ref(18)return {...toRefs(state),age}}
});
</script>

5. computed()

該函數用來創造計算屬性,和過去一樣,它返回的值是一個 ref 對象。

里面可以傳方法,或者一個對象,對象中包含 set()、get()方法。

<script>
import { computed, defineComponent, ref } from 'vue';
export default defineComponent({setup(props, context) {const age = ref(18)// 根據 age 的值,創建一個響應式的計算屬性 readOnlyAge,它會根據依賴的 ref 自動計算并返回一個新的 refconst readOnlyAge = computed(() => age.value++) // 19return {age,readOnlyAge}}
});
</script>// 組合式
<script setup>
import { reactive, computed } from 'vue'const author = reactive({name: 'John Doe',books: ['Vue 2 - Advanced Guide','Vue 3 - Basic Guide','Vue 4 - The Mystery']
})// 一個計算屬性 ref
const publishedBooksMessage = computed(() => {return author.books.length > 0 ? 'Yes' : 'No'
})
</script>
<template><p>Has published books:</p><span>{{ publishedBooksMessage }}</span>
</template>

5.1. 通過 set()、get()方法創建一個可讀可寫的計算屬性

<script>
import { computed, defineComponent, ref } from 'vue';
export default defineComponent({setup(props, context) {const age = ref(18)const computedAge = computed({get: () => age.value + 1,set: value => age.value + value})// 為計算屬性賦值的操作,會觸發 set 函數, 觸發 set 函數后,age 的值會被更新age.value = 100return {age,computedAge}}
});
</script>

6. watch() 函數

watch 函數用來偵聽特定的數據源,并在回調函數中執行副作用。

默認情況是懶執行的,也就是說僅在偵聽的源數據變更時才執行回調。

6.1. 監聽用 reactive 聲明的數據源

<script>
import { computed, defineComponent, reactive, toRefs, watch } from 'vue';export default defineComponent({setup(props, context) {const state = reactive({ name: 'vue', age: 10 })watch(() => state.age,(age, preAge) => {console.log(age); // 100console.log(preAge); // 10})// 修改age 時會觸發 watch 的回調, 打印變更前后的值state.age = 100return {...toRefs(state)}}
});
</script>

6.2. 監聽用 ref 聲明的數據源

<script>
import { defineComponent, ref, watch } from 'vue';
export default defineComponent({setup(props, context) {const age = ref(10);watch(age, () => console.log(age.value)); // 100// 修改age 時會觸發watch 的回調, 打印變更后的值age.value = 100return {age}}
});
</script>

6.3. 同時監聽多個值

<script>
import { computed, defineComponent, reactive, toRefs, watch } from 'vue';export default defineComponent({setup(props, context) {const state = reactive({ name: 'vue', age: 10 })watch([() => state.age, () => state.name],([newName, newAge], [oldName, oldAge]) => {console.log(newName);console.log(newAge);console.log(oldName);console.log(oldAge);})// 修改age 時會觸發watch 的回調, 打印變更前后的值, 此時需要注意, 更改其中一個值, 都會執行watch的回調state.age = 100state.name = 'vue3'return {...toRefs(state)}}
});
</script>

6.4. stop 停止監聽

在 setup() 函數內創建的 watch 監視,會在當前組件被銷毀的時候自動停止。

如果想要明確地停止某個監視,可以調用 watch() 函數的返回值即可,語法如下:

<script>
import { set } from 'lodash';
import { computed, defineComponent, reactive, toRefs, watch } from 'vue';
export default defineComponent({setup(props, context) {const state = reactive({ name: 'vue', age: 10 })const stop =  watch([() => state.age, () => state.name],([newName, newAge], [oldName, oldAge]) => {console.log(newName);console.log(newAge);console.log(oldName);console.log(oldAge);})// 修改age 時會觸發 watch 的回調, 打印變更前后的值, 此時需要注意, 更改其中一個值, 都會執行watch的回調state.age = 100state.name = 'vue3'setTimeout(()=> {stop()// 此時修改時, 不會觸發watch 回調state.age = 1000state.name = 'vue3-'}, 1000) // 1秒之后將取消watch的監聽return {...toRefs(state)}}
});
</script>

7. LifeCycle Hooks(新的生命周期)

新版的生命周期函數,可以按需導入到組件中,且只能在 setup() 函數中使用,但是也可以在 setup 外定義,在 setup 中使用\

<script>
import { set } from 'lodash';
import { defineComponent, onBeforeMount, onBeforeUnmount, onBeforeUpdate, onErrorCaptured, onMounted, onUnmounted, onUpdated } from 'vue';
export default defineComponent({setup(props, context) {onBeforeMount(()=> {console.log('beformounted!')})onMounted(() => {console.log('mounted!')})onBeforeUpdate(()=> {console.log('beforupdated!')})onUpdated(() => {console.log('updated!')})onBeforeUnmount(()=> {console.log('beforunmounted!')})onUnmounted(() => {console.log('unmounted!')})onErrorCaptured(()=> {console.log('errorCaptured!')})return {}}
});
</script>

8. Template refs

通過 refs 來獲取真實 dom 元素,這個和 react 的用法一樣,為了獲得對模板內元素或組件實例的引用,我們可以像往常一樣在 setup()中聲明一個 ref 并返回它

<div><div ref="content">第一步, 在dom上面定義, 他會有一個回調</div></div><ul><li>v-for 出來的ref</li><li>可以寫為表達式的形式, 可以推導出vue是如何實現的</li><li>vue2.x的時候v-for不用這么麻煩, 直接寫上去會被組裝成數組</li><li :ref="el => { items[index] = el }" v-for="(item,index) in 6" :key="item">{{item}}</li></ul>

9. vue 的全局配置

通過 vue 實例上 config 來配置,包含 Vue 應用程序全局配置的對象。您可以在掛載應用程序之前修改下面列出的屬性:

您可以在掛載應用程序之前修改下面列出的屬性:

const app = Vue.createApp({})
app.config = {...}//為組件渲染功能和觀察程序期間的未捕獲錯誤分配處理程序。//錯誤和應用程序實例將調用處理程序:
app.config.errorHandler = (err, vm, info) => {}

可以在應用程序內的任何組件實例中訪問的全局屬性,組件的屬性將具有優先權。

這可以代替 Vue 2.xVue.prototype 擴展:

const app = Vue.createApp({})
app.config.globalProperties.$http = 'xxxxxxxxs'

可以在組件用通過 getCurrentInstance() 來獲取全局 globalProperties 中配置的信息,getCurrentInstance() 方法獲取當前組件的實例,然后通過 ctx 屬性獲得當前上下文,這樣我們就能在 setup 中使用 router 和 vuex,通過這個屬性我們就可以操作變量、全局屬性、組件屬性等等。

setup( ) {   const { ctx } = getCurrentInstance();   ctx.$http }

10. Suspense組件

在開始介紹 Vue 的 Suspense 組件之前,我們有必要先了解一下 React 的 Suspense 組件,因為他們的功能類似。

React.lazy 接受一個函數,這個函數需要動態調用 import()。

它必須返回一個 Promise,該 Promise 需要 resolve 一個 default export 的 React 組件。

import React, { Suspense } from 'react';
const myComponent = React.lazy(() => import('./Component'));
function MyComponent() {return (<div><Suspense fallback={<div>Loading...</div>}><myComponent /></Suspense></div>);
}

Vue3 也新增了 React.lazy 類似功能的 defineAsyncComponent 函數,處理動態引入(的組件)。

defineAsyncComponent 可以接受返回承諾的工廠函數。

當您從服務器檢索到組件定義時,應該調用 Promise 的解析回調。

您還可以調用 reject(reason) 來指示負載已經失敗。

import { defineAsyncComponent } from 'vue'
const AsyncComp = defineAsyncComponent(() =>import('./components/AsyncComponent.vue')
)
app.component('async-component', AsyncComp)

Vue3 也新增了 Suspense 組件:

  1. 異步組件加載狀態管理:可以通過 Suspense 組件來指定一個 loading 插槽,在異步組件加載過程中
  2. 錯誤處理:可以在 Suspense 組件中使用 error 插槽來處理異步組件加載過程中可能發生的錯誤,并展示相關信息
  3. 多個異步組件加載狀態管理:能夠同時管理多個異步組件的加載狀態,在任意一個異步組件加載時展示 loading 狀態,而其他未加載的組件仍然保持正常
<template><Suspense><template #default><my-component /></template><template #fallback>Loading ...</template></Suspense>
</template>
<script>import { defineComponent, defineAsyncComponent } from "vue";const MyComponent = defineAsyncComponent(() => import('./Component'));
export default defineComponent({components: {MyComponent},setup() {return {}}
})
</script>

11. vuex, router, vue 初始化寫法的變化

11.1. vue

import { createApp } from 'vue';
import App from './App.vue'
import router from './router'
import store from './store'// 方法一. 創建實例變成了鏈式, 直接寫下去感覺語義與結構有點模糊, 但是我們要理解vue這樣做的良苦用心, 前端趨近于函數化。
// createApp(App).use(router).use(store).mount('#app')// 方法二.
const app = createApp(App);
app.use(router);
app.use(store);
app.mount('#app');

11.2. router

history 關鍵字:createWebHistory

history模式直接指向history對象,它表示當前窗口的瀏覽歷史,history對象保存了當前窗口訪問過的所有頁面網址。URL中沒有#,它使用的是傳統的路由分發模式,即用戶在輸入一個URL時,服務器會接收這個請求,并解析這個URL,然后做出相應的邏輯處理。

11.2.1. 特點:

當使用history模式時,URL就像這樣:hhh.com/user/id。相比hash模式更加好看。

雖然history模式不需要#。但是,它也有自己的缺點,就是在刷新頁面的時候,如果沒有相應的路由或資源,就會刷出404來。

history api可以分為兩大部分,切換歷史狀態 和 修改歷史狀態:

import { createRouter, createWebHistory } from 'vue-router';
import Home from '../views/Home.vue'const routes = [{path: '/',name: 'Home',component: Home}
]const router = createRouter({// 專門創建history的函數history: createWebHistory(process.env.BASE_URL),routes
})export default router
11.2.2. 使用
<template><div>{{id}}</div>
</template><script>
import { getCurrentInstance, ref } from 'vue';
export default {setup(){const { ctx } = getCurrentInstance()// 1. 這樣也是為了去掉this// 2. 方便類型推導console.log(ctx.$router); // push等方法console.log(ctx.$router.currentRoute.value); // 路由實例// 這個其實沒有必要變成ref因為這個值沒必要動態// 但是他太長了, 這個真的不能忍const id = ref(ctx.$router.currentRoute.value.query.id)// 4: 頁面攔截器ctx.$router.beforeEach((to, from,next)=>{console.log('路由的生命周期')next()})return {id}}
}
</script>

12. vuex

import { createStore } from 'vuex'
// 專門創建實例的一個方法
export default createStore({state: {},mutations: {},actions: {},modules: {}
});

12.1. 使用

import { createStore } from 'vuex'// 難道前端趨勢只有函數這一種嗎
export default createStore({state: {name:'牛逼, 你拿到我了',age: 24,a:'白',b:'黑'},mutations: {updateName(state, n){state.name += n}},actions: {deferName(store) {setTimeout(()=>{// 必須只有commit可以修改值, 這個設定我比較反對, 可以討論// vuex本身結構就很拖沓, 定義域使用個人都不喜歡store.state.name = '牛逼, 你改回來了'},1000)}},getters: {fullName(state){ return `${state.a} - + -${state.b}` }},modules: {}
});
<template><div><p>{{name}}</p><button @click="updateName('+')">點擊改變名字</button><button @click="deferName('+')">改回來</button><p>{{fullName}}</p></div>
</template><script>
import { useStore } from "vuex";
import { computed } from "vue";
export default {setup() {const store = useStore();// 1: 單個引入const name = computed(() => store.state.name);// 2: 引入整個stateconst state = computed(() => store.state);console.log("vuex的實例", state.value); // 別忘了.value// 3: 方法其實就直接從本體上取下來了const updateName = newName => store.commit("updateName", newName);// 4: action一個意思const deferName = () => store.dispatch("deferName");// 5: getter 沒變化const fullName = computed(() => store.getters.fullName);return {name,fullName,deferName,updateName,};}
};
</script>

13. 組件注入

13.1. 父組件

<template><div>組件:<zj :type="type" @ok="wancheng"></zj></div>
</template><script>
import zj from "../components/子組件.vue";
import { ref } from 'vue';
import { provide } from 'vue'export default {components: { zj},setup() {provide('name','向下傳值'); // 基礎值provide('name2', ref('向下傳值')); // 監控值const type = ref('大多數');function wancheng(msg){console.log('子組件-->',msg)setTimeout(()=>{type.value = 'xxxxxxx'},2000)}return {type,wancheng}}
};
</script>

13.2. 子組件

<template><div>props的屬性不用setup去return --- {{type}}</div>
</template><script>
import { inject, ref } from 'vue'
export default {props: {type: String},// 1: props也是不可以解構的, 會失去響應式// 2: context是上下文, 我們可以獲取到slots emit 等方法// 3: props, context 分開也是為了ts更明確的類型推導// setup({type}){setup(props, context) {// 1: propsconsole.log("props", props.type);console.log("上下文", context);context.emit('ok','傳遞完成');// 2: 注入console.log('inject',inject('name'));console.log('inject',inject('xxxx','我是默認值'))inject('name1', ref('默認值')) // 接收方也可以這樣}
};
</script>

14. vue 3.x 完整組件模版結構

一個完成的 vue 3.x 完整組件模版結構包含了:組件名稱、 props、components、setup(hooks、computed、watch、methods 等)

<template><div class="mine" ref="elmRefs"><span>{{name}}</span><br><span>{{count}}</span><div><button @click="handleClick">測試按鈕</button></div><ul><li v-for="item in list" :key="item.id">{{item.name}}</li></ul></div>
</template>
<script lang="ts">
import { computed, defineComponent, getCurrentInstance, onMounted, PropType, reactive, ref, toRefs } from 'vue';
interface IState {count: 0,name: string,list: Array<object>
}
export default defineComponent({name: 'demo',// 父組件傳子組件參數props: {name: {type: String as PropType<null | ''>,default: 'vue3.x'},list: {type: Array as PropType<object[]>,default: () => []}},components: {/// TODO 組件注冊},emits: ["emits-name"], // 為了提示作用setup (props, context) {console.log(props.name)console.log(props.list)const state = reactive<IState>({name: 'vue 3.0 組件',count: 0,list: [{name: 'vue',id: 1},{name: 'vuex',id: 2}]})const a = computed(() => state.name)onMounted(() => {})function handleClick () {state.count ++// 調用父組件的方法context.emit('emits-name', state.count)}return {...toRefs(state),handleClick}}
});
</script>

在通往幸福道路上,并沒有什么捷徑可走,唯有付出努力和拼搏

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

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

相關文章

函數——遞歸6(c++)

角谷猜想 題目描述 日本一位中學生發現一個奇妙的 定理&#xff0c;請角谷教授證明&#xff0c;而教授 無能為力&#xff0c;于是產生了角谷猜想。 猜想的內容&#xff1a;任給一個自然數&#xff0c; 若為偶數則除以2&#xff0c;若為奇數則乘 3加1&#xff0c;得到一個新的…

git命令整理

一、什么是git Git 是為了幫助管理 Linux 內核開發而開發的一個開放源碼的版本控制軟件。 分布式管理系統&#xff0c;可以快速的查看文件各個版本的改動。比如在第5行加了一個單詞“Linux”&#xff0c;在第8行刪了一個單詞“Windows”。而圖片、視頻這些二進制文件&#xf…

PyTorch深度學習快速入門

PyTorch深度學習快速入門 1.PyTorch環境配置及安裝2.python編輯器的選擇、安裝、配置&#xff08;pycharm、JupyTer安裝&#xff09;3.為什么torch.cuda.is_available()返回false4.python學習中兩大法寶函數&#xff08;也可用在pytorch&#xff09;5.pycharm和jupyter&#xf…

golang goroutine 如何退出?

上一講說到調度器將maingoroutine推上舞臺&#xff0c;為它鋪好了道路&#xff0c;開始執行runtime.main函數。這一講&#xff0c;我們探索maingoroutine以及普通goroutine從執行到退出的整個過程。 //Themaingoroutine. funcmain(){ //gmaingoroutine&#xff0c;不再是g0了 …

Python列表中添加刪除元素不走彎路

1.append() 向列表中添加單個元素&#xff0c;一般用于尾部追加 list1 ["香妃", "乾隆", "賈南風", "趙飛燕", "漢武帝"]list1.append("周瑜") print(list1) # [香妃, 乾隆, 賈南風, 趙飛燕, 漢武帝, 周瑜]…

STM32標準庫——(14)I2C通信協議、MPU6050簡介

1.I2C通信 I2C 通訊協議(Inter&#xff0d;Integrated Circuit)是由Phiilps公司開發的&#xff0c;由于它引腳少&#xff0c;硬件實現簡單&#xff0c;可擴展性強&#xff0c; 不需要USART、CAN等通訊協議的外部收發設備&#xff0c;現在被廣泛地使用在系統內多個集成電路(IC)間…

【LeetCode每日一題】【BFS模版與例題】863.二叉樹中所有距離為 K 的結點

BFS的基本概念 BFS 是廣度優先搜索&#xff08;Breadth-First Search&#xff09;的縮寫&#xff0c;是一種圖遍歷算法。它從給定的起始節點開始&#xff0c;逐層遍歷圖中的節點&#xff0c;直到遍歷到目標節點或者遍歷完所有可達節點。 BFS 算法的核心思想是先訪問當前節點的…

計算機網絡_2.2物理層下面的傳輸媒體

2.2物理層下面的傳輸媒體 一、傳輸媒體的分類二、導向型傳輸媒體1、同軸電纜2、雙絞線3、光纖&#xff08;1&#xff09;光纖通信原理&#xff08;2&#xff09;光纖組成&#xff08;4&#xff09;多模光纖與單模光纖對比&#xff08;5&#xff09;光纖的波長與規格&#xff08…

海量淘寶商品數據如何實現自動化抓取?

隨著電子商務的飛速發展&#xff0c;淘寶作為中國最大的網絡購物平臺之一&#xff0c;其商品數據具有極高的商業價值。然而&#xff0c;如何有效地從海量的淘寶商品數據中抓取所需信息&#xff0c;成為了一個技術挑戰。本文將深入探討如何實現淘寶商品數據的自動化抓取&#xf…

c# using 用法

using命令空間 導入命名空間中的所有類型 如&#xff1a;using System.Text; using別名 using別名包括詳細命名空間信息的具體類型&#xff0c;這種做法有個好處就是當同一個cs引用了兩個不同的命名空間&#xff0c;但兩個命名空間都包括了一個相同名字的類型的時候。當需要…

SQL加鎖機制深度解析:不同隔離級別與索引類型的影響

首先&#xff0c;我們先理解一下涉及的幾個核心概念&#xff1a; 主鍵 (Primary Key): 主鍵是數據庫表中的特殊列&#xff0c;用于唯一標識表中的每一行。它不能有重復值&#xff0c;也不能有NULL值。 唯一索引 (Unique Index): 唯一索引類似于主鍵&#xff0c;但它允許NULL值…

數據可視化基礎與應用-02-基于powerbi實現連鎖糕點店數據集的儀表盤制作

總結 本系列是數據可視化基礎與應用的第02篇&#xff0c;主要介紹基于powerbi實現一個連鎖糕點店數據集的儀表盤制作。 數據集描述 有一個數據集&#xff0c;包含四張工作簿&#xff0c;每個工作簿是一張表&#xff0c;其中可以銷售表可以劃分為事實表&#xff0c;產品表&am…

【Python小技巧】將list變量寫入本地txt文件并讀出為list變量的方法(附代碼)

文章目錄 前言一、萬能的txt和eval大法二、具體代碼和使用方法總結 前言 使用Python&#xff0c;我們偶爾需要將一些變量保存到本地&#xff0c;并被其它代碼讀取作為參數&#xff0c;那么怎么辦呢&#xff1f; 一、萬能的txt和eval大法 這里教大家一個簡單的方法&#xff0c…

912. 排序數組(快速排序)

快速排序&#xff1a; 分&#xff1a;找到分成兩部分進行排序的pos&#xff08;使用partition&#xff09;治&#xff1a;分別對這兩部分進行快速排序 重點&#xff1a;partition 找到pivot&#xff08;兩個方法&#xff1a;1. 取第一個值&#xff1b;2. 取隨機值&#xff09…

Linux時間同步(PPS、PTP、chrony)分析筆記

1 PPS(pulse per second) 1.1 簡介 LinuxPPS provides a programming interface (API) to define in the system several PPS sources. PPS means "pulse per second" and a PPS source is just a device which provides a high precision signal each second so t…

每日一題 2673使二叉樹所有路徑值相等的最小代價

2673. 使二叉樹所有路徑值相等的最小代價 題目描述&#xff1a; 給你一個整數 n 表示一棵 滿二叉樹 里面節點的數目&#xff0c;節點編號從 1 到 n 。根節點編號為 1 &#xff0c;樹中每個非葉子節點 i 都有兩個孩子&#xff0c;分別是左孩子 2 * i 和右孩子 2 * i 1 。 樹…

Java緩存簡介

內存訪問速度和硬盤訪問速度是計算機系統中兩個非常重要的性能指標。 內存訪問速度&#xff1a;內存是計算機中最快的存儲介質&#xff0c;它的訪問速度可以達到幾納秒級別。內存中的數據可以直接被CPU訪問&#xff0c;因此讀寫速度非常快。 硬盤訪問速度&…

學習和工作的投入產出比(節選)

人工智能統領全文 推薦包含關于投入、產出、過剩、市場關注、案例、結果和避雷等主題的信息&#xff1a; 投入與產出&#xff1a; 投入和產出都有直接和間接兩類常見形式。常見的四種組合是&#xff1a;直接投入、直接產出、間接投入、間接產出。 過剩&#xff1a; 過剩是一個重…

力扣SQL50 無效的推文 查詢

Problem: 1683. 無效的推文 思路 &#x1f468;?&#x1f3eb; 參考 char_length(str)&#xff1a;計算 str 的字符長度length(str)&#xff1a;計算 str 的字節長度 Code select tweet_id from Tweets where char_length(content) > 15;

C++與 Fluke5500A設備通過GPIB-USB-B通信的經驗積累

C與 Fluke5500A設備通過GPIB-USB-B通信的經驗積累 以下內容來自&#xff1a;C與 Fluke5500A設備通過GPIB-USB-B通信的經驗積累 - JMarcus - 博客園 (cnblogs.com)START 1.需要安裝NI-488.2.281&#xff0c;安裝好了之后&#xff0c;GPIB-USB-B的驅動就自動安裝好了 注意版本…