日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

Vue3 前端開發(fā)時(shí),需要api請(qǐng)求模擬和數(shù)據(jù)模擬,我將在接下來的時(shí)間寫Mock模擬和json-server模擬。兩種比較流行的模式,現(xiàn)在先介紹mock模式。

使用NPM安裝Mock.js庫

Mock.js是一個(gè)輕量級(jí)的前端數(shù)據(jù)模擬工具,可以快速生成隨機(jī)數(shù)據(jù),支持自定義數(shù)據(jù)類型和規(guī)則等功能。我們可以使用NPM來進(jìn)行安裝。

在命令行中執(zhí)行以下命令:

npm install mockjs
yarn add mockjs  -D

在Vue項(xiàng)目中配置Mock.js

在Vue項(xiàng)目中使用Mock.js需要先引入相關(guān)的包,并在項(xiàng)目中進(jìn)行相應(yīng)的配置。我們可以先在Vue項(xiàng)目中創(chuàng)建一個(gè) mock 文件夾,并在該文件夾下新建index.ts文件。
在index.ts文件(src/mock/index.ts)中編寫以下代碼:

import Mock from 'mockjs'
// 設(shè)置攔截Ajax請(qǐng)求的響應(yīng)時(shí)間,模擬網(wǎng)絡(luò)延遲
Mock.setup({
  timeout: '200-600'
})
// Mock數(shù)據(jù)示例
Mock.mock(baseUrl + '/api/getList', 'get', {
    'list|10-20': [{
        'id|+1': 1,
        'title': '@ctitle(10, 20)',
        'image': '@image('200x100', '#50B347', 'Hello')',
        'datetime': '@datetime()',
        'author': '@cname()',
        'description': '@ctitle(30, 100)'
    }]
})
export default Mock

以上示例代碼表示:當(dāng)訪問/api/getList接口時(shí),Mock.js會(huì)返回一個(gè)長(zhǎng)度在10-20之間的數(shù)組,數(shù)組中每一項(xiàng)都包含id、title、image、datetime、author和description屬性,并使用@ctitle、@image、@datetime和@cname等方法生成隨機(jī)數(shù)據(jù)。

創(chuàng)建http請(qǐng)求對(duì)象
src/utils/request.ts

import axIOS from 'axios'


const service = axios.create({
    baseURL: '/ceshi',
    withCredentials: false,
    timeout: 60000
})

// 請(qǐng)求攔截器
service.interceptors.request.use(config => {
    // ...統(tǒng)一修改請(qǐng)求頭
    return config
}, error => {
    // ...網(wǎng)絡(luò)錯(cuò)誤
    Promise.reject(error)
})

// 響應(yīng)攔截
service.interceptors.response.use(response => {
    return response.data
}, e => {
    return Promise.reject(e)
})

export default service

創(chuàng)建視圖
src/views/MockView.vue

<template>
    <div>
        <ul>
            <li v-for="item in list" :key="item.id">
                <h5>
                    {{ item.tip }}? {{ item.content }}
                </h5>
            </li>
        </ul>
    </div>
    <el-table :data="tableList" style="width: 100%">
        <el-table-column prop="id" label="ID" width="30" />
        <el-table-column prop="name" label="姓名" width="120" />
        <el-table-column prop="age" label="年齡" width="80" />
        <el-table-column prop="date" label="日期" width="180" />
        <el-table-column prop="emAIl" label="郵箱" width="280" />
        <el-table-column prop="city" label="城市" width="180" />
    </el-table>

    <el-table :data="AuthorList" style="width: 100%">
        <el-table-column prop="id" label="ID" width="50" />
        <el-table-column prop="author" label="作者" width="120" />
        <el-table-column prop="title" label="標(biāo)題" width="80" />
        <el-table-column prop="datetime" label="時(shí)間" width="180" />
        <el-table-column  label="圖片" width="180">
            <template #default="scope">
                <el-image :src="scope.row.image"></el-image>
            </template>
        </el-table-column>
        <el-table-column prop="description" label="說明" width="180" />
        <el-table-column fixed="right" label="Operations" width="120">
            <template #default>
                <el-button link type="primary" size="small" @click="fun_del">Detail</el-button>
                <el-button link type="primary" size="small" @click="fun_edit">Edit</el-button>
            </template>
        </el-table-column>
    </el-table>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import request from '@/utils/request'
type TArticle = {
    id: number,
    tip: string,
    content: string,
};
interface Article {
    id: number,
    tip: string,
    content: string,
};

let list = ref<Array<TArticle>>([]);
const tableList = ref([])
const AuthorList = ref([])

onMounted(() => {
    /** */
    getList()
        .then((res) => {
            AuthorList.value = res.list;
            console.log(res);
        });

    getDetail(1)
        .then((res) => {
            console.log(res);
            tableList.value = res.data;
        })
        .catch((e) => {
            console.error(e)
        })
})

const fun_del = (data: any) => {
    console.log(data);
}
const fun_edit = (data: any) => {
    console.log(data);
}
const fun_add = (data: any) => {
    console.log(data);
}

const getList = async () => {
    return request({
        url: '/api/getList',
        method: 'get'
    })
}

function getArticles() {
    return request({
        url: '/api/article',
        method: 'get'
    })
}
function selectA(val: any) {
    return request({
        url: '/api/selectArticle',
        method: 'get',
        data: {
            val
        }
    })
}

const getDetail = async (id: Number) => {
    const ret = await request({
        url: '/api/getDetail',
        method: 'post',
        data: {
            ID: id,
        }
    })
    return ret;
}
</script>

路由 src/router/index.ts 代碼

import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'

// 頁面組件通過import引入,如果使用懶加載方式則無需在此通過import引入
// import HomeView from '../views/HomeView.vue'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
    //createWebHashHistory(), // history 模式則使用 createWebHistory()
    history: createWebHashHistory(import.meta.env.BASE_URL),
    routes: [
        // 組件引用方式有兩種,只需選擇一種方式即可
        {
            path: '/',
            name: 'home',
            component: HomeView
        },
        {
            path: '/about',
            name: 'about',
            // route level code-splitting
            // this generates a separate chunk (About.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import('../views/AboutView.vue')
        },
        {
            path: '/mock',
            name: 'mock',
            component: () => import('../views/MockView.vue')
        }
    ]
})

修改src/main.ts

import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
//import Mock from './mock' // 引入mock數(shù)據(jù)
import "./mock/index.ts"
const app =  createApp(App)
app.use(router)
//整體引入
app.use(ElementPlus,  { size: 'small', zIndex: 3000 })
app.mount('#app')

修改 App.vue 文件

<template>
  <div class="wrapper">
    <nav>
      <router-link to="/">home</router-link> |
      <router-link to="/mock">mock</router-link> |
      <router-link to="/about">About</router-link>
    </nav>
  </div>
  <Router-view />
  <!-- <HelloWorld msg="You did it!" /> -->
</template>

<script setup lang="ts">
//import HelloWorld from './components/HelloWorld.vue'
</script>

<style lang="less" scoped>
.logo {
  height: 6em;
  padding: 1.5em;
  will-change: filter;
  transition: filter 300ms;
}

.logo:hover {
  filter: drop-shadow(0 0 2em #646cffaa);
}

.logo.vue:hover {
  filter: drop-shadow(0 0 2em #42b883aa);
}

#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

nav {
  padding: 30px;

  a {
    font-weight: bold;
    color: #2c3e50;

    &.router-link-exact-active {
      color: #42b983;
    }
  }
}
</style>

補(bǔ)充

ElementPlusg官網(wǎng)參考:
https://element-plus.org/zh-CN/guide/quickstart.html
推薦#
自動(dòng)導(dǎo)入
首先你需要安裝unplugin-vue-components 和 unplugin-auto-import這兩款插件

npm install -D unplugin-vue-components unplugin-auto-import

Vite#
然后把下列代碼插入到你的 Vite 的配置文件中

// vite.config.ts
import { defineConfig } from 'vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default defineConfig({
  // ...
  plugins: [
    // ...
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
})

 

效果圖

 

深入詳解Vue3中的Mock數(shù)據(jù)模擬

 

目錄結(jié)構(gòu)

 

深入詳解Vue3中的Mock數(shù)據(jù)模擬

 

package.json 文件

{
  "name": "vue3-vite-ts",
  "private": true,
  "version": "0.0.0.1",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vue-tsc && vite build",
    "preview": "vite preview",
    "lint": "eslint src/**/*.{js,jsx,vue,ts,tsx} --fix"
  },
  "dependencies": {
    "@element-plus/icons-vue": "^2.1.0",
    "axios": "^1.5.0",
    "babel-eslint": "^10.1.0",
    "element-plus": "^2.4.2",
    "http": "^0.0.1-security",
    "js-cookie": "^3.0.5",
    "path": "^0.12.7",
    "pinia": "^2.1.6",
    "pinia-plugin-persistedstate": "^3.2.0",
    "pinia-use-persist": "^0.0.21",
    "qs": "^6.11.2",
    "vite-plugin-svg-icons": "^2.0.1",
    "vue": "^3.3.4",
    "vue-router": "^4.2.4"
  },
  "devDependencies": {
    "@types/js-cookie": "^3.0.4",
    "@types/mockjs": "^1.0.9",
    "@types/node": "^20.6.3",
    "@types/nprogress": "^0.2.0",
    "@types/qs": "^6.9.9",
    "@typescript-eslint/eslint-plugin": "^6.7.2",
    "@typescript-eslint/parser": "^6.7.2",
    "@vitejs/plugin-vue": "^4.2.3",
    "eslint": "^8.49.0",
    "eslint-plugin-vue": "^9.17.0",
    "fast-glob": "^3.3.1",
    "fs": "^0.0.1-security",
    "less": "^4.2.0",
    "less-loader": "^11.1.3",
    "mockjs": "^1.1.0",
    "nprogress": "^0.2.0",
    "sass": "^1.68.0",
    "typescript": "^5.0.2",
    "unplugin-auto-import": "^0.16.7",
    "unplugin-vue-components": "^0.25.2",
    "vite": "^4.4.5",
    "vite-plugin-svg-icons": "^2.0.1",
    "vue-tsc": "^1.8.5"
  },
  "rules": {
    "vue/multi-word-component-names": "off"
  }
}

分享到:
標(biāo)簽:Vue3
用戶無頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定