vue3中如何使用ts
如何使用ts
在創(chuàng)建vue腳手架的時(shí)候吧typescript選上
app.vue
<template> ? <!-- <div id="nav"> ? ? <router-link to="/">Home</router-link> | ? ? <router-link to="/about">About</router-link> ? </div> ? <router-view/> --> ? <div id="root"> ? ? <div className="todo-container"> ? ? ? <div className="todo-wrap"> ? ? ? ? <Header :addtodo="addtodo"/> ? ? ? ? <List :todos="state.todos" /> ? ? ? ? <Footer /> ? ? ? </div> ? ? </div> ? </div> </template>
<script lang="ts">
import { defineComponent, reactive } from "vue";
import Footer from "./components/footer/footer.vue";
import Header from "./components/header/header.vue";
import List from "./components/list/list.vue";
//導(dǎo)入接口類型
import {todo} from "./type/todo"
export default defineComponent({
? components: { List, Header, Footer },
? setup() {
? ? const state = reactive({
? ? ? todos: [
? ? ? ? { id: 1, title: "吃飯", isture: true },
? ? ? ? { id: 2, title: "睡覺(jué)", isture: false },
? ? ? ? { id: 3, title: "打游戲", isture: false },
? ? ? ? { id: 4, title: "打代碼", isture: true },
? ? ? ],
? ? });
? ? //定義一個(gè)方法用來(lái)接收輸入框里面?zhèn)鱽?lái)的數(shù)據(jù)并把它加到state.todos里面面
? ? const addtodo=(todo:todo)=>{
? ? ? state.todos.unshift(todo)
? ? }
? ? return {
? ? ? state,
? ? ? addtodo
? ? };
? },
});
</script><style> @import "./App.css"; </style>
header.vue
<template> ? <div class="todo-header"> ? ? <input ? ? ? type="text" ? ? ? placeholder="請(qǐng)輸入你的任務(wù)名稱,按回車鍵確認(rèn)" ? ? ? v-model="title" ? ? ? @keyup.enter="add" ? ? /> ? </div> </template>
<style scoped> @import "./header.css"; </style>
<script lang="ts">
import { defineComponent, ref } from "vue";
//導(dǎo)入接口類型
import { todo } from "../../type/todo";
export default defineComponent({
? props: {
? ? addtodo: {
? ? ? type: Function,
? ? ? required: true,
? ? },
? },
? setup(props) {
? ? const title = ref("");
? ? const add = () => {
? ? ? if (title.value == "") {
? ? ? ? alert("請(qǐng)輸入內(nèi)容");
? ? ? }
? ? ? const todo: todo = {
? ? ? ? id: Date.now(),
? ? ? ? title: title.value,
? ? ? ? isture: false,
? ? ? };
? ? ? props.addtodo(todo);
? ? ? title.value=''
? ? };
? ? return {
? ? ? add,
? ? ? title,
? ? };
? },
});
</script>list.vue
<template> ? ? <ul className="todo-main"> ? ? ? ? ?<Listitem v-for="(todos,index) in todos" :key="todos.id" :todos='todos'></Listitem> ? ? </ul> </template>
<style scoped> @import"./list.css"; </style>
<script lang="ts">
import Listitem from "./listitem/listitem.vue"
import { defineComponent } from 'vue'
export default defineComponent({
? ? components:{
? ? ? ? Listitem
? ? },
? ? props:{
? ? ? ? todos:Object
? ? }
? ? ,
? ? setup() {
? ? ? ??
? ? },
})
</script>listitem.vue
<template>
?
? ? <li ?class="itli" >
? ? ? <label>
? ? ? ? <input type="checkbox" v-model="todos.isture" />
? ? ? ? <span>{{todos.title}}</span>
? ? ? </label>
? ? ? <button
? ? ? ? class="btn btn-danger"
? ? ? ? ? ? ?>刪除</button>
? ? </li>
?
</template><style scoped> @import "./listitem.css"; </style>
<script lang="ts">
import { defineComponent } from "vue";
//導(dǎo)入接口類型
import {todo} from "../../../type/todo"
export default defineComponent({
? setup() {},
? props:{
//定義todos的類型是自己定義的todo接口
? ? todos:Object as ()=>todo
? }
});
</script>footer.vue
<template> ? ? <div class="todo-footer"> ? ? ? ? <label> ? ? ? ? ? <input type="checkbox" ? ? /> ? ? ? ? </label> ? ? ? ? <span ?> ? ? ? ? ? <span >已完 </span> / 全部? ? ? ? ? </span> ? ? ? ? <button class="btn btn-danger" ?>清除已完成任務(wù)</button> ? ? ? </div> </template>
<style scoped> /* @import"./footer.css"; */ </style>
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
? ? setup() {
? ? ? ??
? ? },
})
</script>






以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue-cli3+typescript新建一個(gè)項(xiàng)目的思路分析
這篇文章主要介紹了vue-cli3+typescript新建一個(gè)項(xiàng)目的思路,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
vue?this.$router.go(-1);返回時(shí)如何帶參數(shù)
這篇文章主要介紹了vue?this.$router.go(-1);返回時(shí)如何帶參數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Vue中子組件不能修改父組件傳來(lái)的Prop值的原因分析
在Vue中,父子組件之間通過(guò)Prop和Event實(shí)現(xiàn)了數(shù)據(jù)的雙向綁定,但是,Vue設(shè)計(jì)者為什么不允許子組件修改父組件傳遞的Prop呢,本文就來(lái)帶大家探究為什么子組件不能修改Prop,需要的朋友可以參考下2023-06-06
windows下vue.js開(kāi)發(fā)環(huán)境搭建教程
這篇文章主要為大家詳細(xì)介紹了windows下vue.js開(kāi)發(fā)環(huán)境搭建教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Vue使用正則校驗(yàn)文本框?yàn)檎麛?shù)
這篇文章主要介紹了Vue使用正則校驗(yàn)文本框?yàn)檎麛?shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue?首頁(yè)加載,速度優(yōu)化及解決首頁(yè)白屏的問(wèn)題
這篇文章主要介紹了vue?首頁(yè)加載,速度優(yōu)化及解決首頁(yè)白屏的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2022-10-10
vue3中?provide?和?inject?用法小結(jié)
父子組件傳遞數(shù)據(jù)時(shí),使用的是props和emit,父?jìng)髯訒r(shí),使用的是?props,如果是父組件傳孫組件時(shí),就需要先傳給子組件,子組件再傳給孫組件,如果多個(gè)子組件或多個(gè)孫組件使用時(shí),就需要傳很多次,會(huì)很麻煩,這篇文章主要介紹了vue3中?provide?和?inject?用法,需要的朋友可以參考下2023-11-11
基于vue中keep-alive緩存問(wèn)題的解決方法
今天小編就為大家分享一篇基于vue中keep-alive緩存問(wèn)題的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09

