From 61cbd43b31251389b2d2357306444cb7627e8120 Mon Sep 17 00:00:00 2001 From: xiao Date: Sun, 19 May 2024 21:24:42 +0800 Subject: [PATCH] tj --- .../20240513 \345\244\215\344\271\240.md" | 311 ++++++++++++++++++ .../20240514 pinia.md" | 56 ++++ 2 files changed, 367 insertions(+) create mode 100644 "\350\202\226\346\226\207\346\205\247/20240513 \345\244\215\344\271\240.md" create mode 100644 "\350\202\226\346\226\207\346\205\247/20240514 pinia.md" diff --git "a/\350\202\226\346\226\207\346\205\247/20240513 \345\244\215\344\271\240.md" "b/\350\202\226\346\226\207\346\205\247/20240513 \345\244\215\344\271\240.md" new file mode 100644 index 0000000..4c3513e --- /dev/null +++ "b/\350\202\226\346\226\207\346\205\247/20240513 \345\244\215\344\271\240.md" @@ -0,0 +1,311 @@ +```js +import koa from 'koa' +import bodyparser from 'koa-bodyparser' +import cors from 'koa2-cors' +import { DataTypes, Sequelize, Op } from 'sequelize' +import Router from 'koa-router' + + +let app = new koa() +let router = new Router() +let prop = 3000 + +let sql = new Sequelize('blog','sa','123456',{ + host:'localhost', + dialect:'mssql' +}) + +let data = sql.define('blogs',{ + title:{type:DataTypes.STRING}, + type:{type:DataTypes.STRING}, + name:{type:DataTypes.STRING}, + age:{type:DataTypes.INTEGER} +}) +await data.sync() + +app.use(cors()) +app.use(bodyparser()) +router.get('/book/:id?',async (cxk)=>{ + let id = cxk.params.id||0 + let xs + if (id>0) { + let item = await data.findByPk(id) + xs= item + }else{ + let item = await data.findAll() + xs= item + } + cxk.body={ + code:200, + data:xs, + msg:'获取成功' + } +}) +router.get('/sel',async (cxk)=>{ + let keyword = cxk.request.query.keyword.trim() + let sel = await data.findAll({ + where:{ + [Op.or]:{ + title:{[Op.like]:`%${keyword}%`}, + type:{[Op.like]:`%${keyword}%`}, + name:{[Op.like]:`%${keyword}%`} + } + } + }) + cxk.body={ + code:200, + data:sel, + msg:'查询成功' + } + +}) +router.post('/add',async (cxk)=>{ + let obj = cxk.request.body + let add = await data.create(obj) + cxk.body={ + code:200, + data:add, + msg:'获取成功' + } +}) + +router.put('/edit/:id?',async (cxk)=>{ + let id = cxk.params.id + let obj = cxk.request.body + await data.update(obj,{ + where:{ + id:id + } + }) + cxk.body={ + code:200, + data:obj, + msg:'修改成功' + } +}) +router.delete('/del/:id',async (cxk)=>{ + let id = cxk.params.id + + await data.destroy({ + where:{ + id:id + } + }) + cxk.body={ + code:200, + data:[], + msg:'删除成功' + } +}) +app.use(router.routes()) + + + +app.listen(prop,()=>{ + console.log(`http://localhost:${prop}`); +}) +前端 + + + + + +``` \ No newline at end of file diff --git "a/\350\202\226\346\226\207\346\205\247/20240514 pinia.md" "b/\350\202\226\346\226\207\346\205\247/20240514 pinia.md" new file mode 100644 index 0000000..7cf1b8c --- /dev/null +++ "b/\350\202\226\346\226\207\346\205\247/20240514 pinia.md" @@ -0,0 +1,56 @@ +### Pinia 是一个为 Vue 3 设计的状态管理库,具有简单易用的 API 和出色的性能。 + + 安装 Pinia + 可以通过 npm 或 yarn 安装 Pinia: +```js +npm install pinia +或 + +yarn add pinia +``` + +### 创建 Pinia Store +要创建一个 Pinia Store,需要引入 defineStore 函数,并定义一个 Store 类。例如: + +```js +import { defineStore } from 'pinia'; + +export const useCounterStore = defineStore({ + id: 'counter', + state: () => ({ + count: 0, + }), + actions: { + increment() { + this.count++; + }, + decrement() { + this.count--; + }, + }, +}); +在 Vue 组件中使用 Pinia Store +在 Vue 组件中使用 Pinia Store 非常简单。首先需要在应用程序的入口处安装 Pinia: + +import { createApp } from 'vue'; +import App from './App.vue'; +import { createPinia } from 'pinia'; + +const app = createApp(App); +app.use(createPinia()); +app.mount('#app'); +然后可以在组件中使用 useStore 方法访问 Pinia Store: + +import { defineComponent } from 'vue'; +import { useCounterStore } from './store'; + +export default defineComponent({ + setup() { + const counterStore = useCounterStore(); + + return { + counterStore, + }; + }, +}); +``` \ No newline at end of file -- Gitee