​从新回归Vue之3.0(四):动态组件,vuex

一,component动态组件

由于组件被引用为变量而不是作为字符串键来注册的,在 <script setup> 中要使用动态组件的时候,就应该使用动态的 :is 来绑定:

<script setup lang='ts'>
import Foo from './Foo.vue'
import Bar from './Bar.vue'
</script>

<template>
  <component :is="Foo" />
  <component :is="someCondition ? Foo : Bar" />
</template>

二,ts限制普通函数/箭头函数参数类型

<script setup lang="ts">
   	function test(params:(string|boolean)):void {
        console.log(params);
    }
    test('5555')
</script>
<script setup lang="ts">
    const test = (params:(string|boolean))=>{
        console.log(params)
    }
    test('5555')
</script>

三,引入vuex配置和使用

npm install vuex@next --save

main.ts

import { createApp } from 'vue'
import App from './App.vue'
// 导入store模块, 传入 injection key
import store from './store';

const app = createApp(App)
app.use(store)
app.mount('#app')

store文件夹下index.ts

// 引入
import { createStore } from "vuex";

export default createStore({
  // 声明变量
  state: {
    "name": 'xxxxx'
  },
  // 修改变量(state不能直接赋值修改,只能通过mutations)
  mutations: {
    setName(state, newValue){
      state.name = newValue
    }
  },
  actions: {},
  modules: {},
});

vuex.vue测试文件

<template>
  <button @click="changeName" size="small">点击修改名称</button>
</template>

<script setup lang="ts">
import { ref, reactive, watch, onMounted, computed } from "vue";
import { useStore } from 'vuex'
// data
const store = useStore()
let name = computed(()=>{ return store.state.name });
// props
// emit
// methods
function changeName():void{
    store.commit('setName', '哈哈哈')
    console.log('修改后的名称:'+name.value);
}
//watch
// defineExpose
// 生命周期
onMounted(() => {
  console.log(name.value)
});

</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值