vue3插件json2ts的使用

1、安装vscode插件json2ts

右键选中插件,修改快捷键方式 ctrl+shift+alt+v

2、复制json格式的数据

2.1、在nuxt中可以使用如下方式copyjson格式数据

{
  "aid": 701297313,
  "type_id": 182,
  "tname": "影视杂谈",
  "pic": "http://i0.hdslb.com/bfs/archive/4e0981141ac047f06118a30f4af322d45f4ce63c.jpg",
  "title": "一口气看完美剧大片《星期三》完整版",
  "pubdate": 1690181679,
  "ctime": 1690181679,
  "tags": [],
  "duration": 2208,
  "author": {
    "mid": 1446209135,
    "name": "番茄君来了",
    "face": "https://i2.hdslb.com/bfs/face/0db953a00ded43b1eec3539b99cd63294ead1283.jpg"
  },
  "stat": {
    "aid": 701297313,
    "view": 362915,
    "danmaku": 392,
    "reply": 171,
    "favorite": 3905,
    "coin": 643,
    "share": 182,
    "now_rank": 0,
    "his_rank": 0,
    "like": 12383,
    "dislike": 0,
    "vt": 0,
    "vv": 362915
  },
  "hot_desc": "",
  "corner_mark": 0,
  "bvid": "BV1Hm4y1L74g",
  "enable_vt": 0
}

3、新建一个ts文件

执行如下快捷键:ctrl+shift+alt+v 

export interface Author {
	mid: number;
	name: string;
	face: string;
}

export interface Stat {
	aid: number;
	view: number;
	danmaku: number;
	reply: number;
	favorite: number;
	coin: number;
	share: number;
	now_rank: number;
	his_rank: number;
	like: number;
	dislike: number;
	vt: number;
	vv: number;
}

export interface RootObject {
	aid: number;
	type_id: number;
	tname: string;
	pic: string;
	title: string;
	pubdate: number;
	ctime: number;
	tags: any[];
	duration: number;
	author: Author;
	stat: Stat;
	hot_desc: string;
	corner_mark: number;
	bvid: string;
	enable_vt: number;
}

 vue3插件json2ts - 掘金 (juejin.cn)

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3和Typescript的使用方式主要有以下几种: 1. 使用Vue CLI创建项目时选择TypeScript作为语言 在创建Vue项目时,可以选择使用TypeScript作为项目的语言。这样可以直接在项目中使用TypeScript,并且会自动生成一些TypeScript相关的配置文件和代码结构。具体步骤如下: ``` vue create my-project ``` 在选择项目配置时选择"Manually select features",然后选择TypeScript即可。 2. 在已有的Vue项目中添加TypeScript支持 如果已经有一个Vue项目,也可以在其中添加TypeScript支持。具体步骤如下: - 安装TypeScript和相关依赖 ``` npm install --save-dev typescript ts-loader @vue/cli-plugin-typescript ``` - 配置TypeScript 在项目根目录下创建一个`tsconfig.json`文件,并添加以下内容: ```json { "compilerOptions": { "target": "esnext", "module": "esnext", "strict": true, "jsx": "preserve", "esModuleInterop": true, "moduleResolution": "node", "allowSyntheticDefaultImports": true, "sourceMap": true, "baseUrl": ".", "paths": { "@/*": ["src/*"] }, "lib": ["esnext", "dom", "dom.iterable", "scripthost"] }, "include": ["src/**/*.ts", "src/**/*.tsx", "tests/**/*.ts", "tests/**/*.tsx"], "exclude": ["node_modules"] } ``` - 配置Vue插件 在`vue.config.js`文件中添加以下内容: ```js module.exports = { pluginOptions: { 'style-resources-loader': { preProcessor: 'scss', patterns: [] } }, configureWebpack: { resolve: { extensions: ['.ts', '.tsx', '.js', '.vue', '.json'] }, module: { rules: [ { test: /\.tsx?$/, loader: 'ts-loader', exclude: /node_modules/, options: { appendTsSuffixTo: [/\.vue$/] } } ] } } } ``` - 修改Vue组件文件后缀名 在Vue组件文件中,将`.vue`后缀名改为`.vue.ts`,这样可以直接在Vue组件中使用TypeScript。 3. 在Vue3中使用TypeScript 在Vue3中,可以使用以下方式来使用TypeScript: - 在Vue组件中使用TypeScript 在Vue3中,可以直接在Vue组件中使用TypeScript。具体步骤如下: - 在`setup()`函数中定义变量的类型 - 在`defineComponent()`函数中使用`defineProps()`和`defineEmits()`定义属性和事件的类型 - 在`template`中使用TypeScript语法 示例代码: ```html <template> <div>{{ count }}</div> </template> <script lang="ts"> import { defineComponent, defineProps, defineEmits } from 'vue' interface Props { msg: string count: number } export default defineComponent({ props: defineProps<Props>(), emits: defineEmits(['update:count']), setup(props) { const count = ref(props.count) return { count } } }) </script> ``` - 在Vue3中使用Composition API Vue3中的Composition API也支持使用TypeScript。具体步骤如下: - 使用`ref()`、`reactive()`等函数定义变量的类型 - 在`defineComponent()`函数中使用`defineProps()`和`defineEmits()`定义属性和事件的类型 示例代码: ```html <template> <div>{{ count }}</div> </template> <script lang="ts"> import { defineComponent, defineProps, defineEmits, ref } from 'vue' interface Props { msg: string count: number } export default defineComponent({ props: defineProps<Props>(), emits: defineEmits(['update:count']), setup(props) { const count = ref(props.count) return { count } } }) </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值