看一些人的 Vue 3 预览教程的一些例子。 [目前是测试版]
我找到了两个例子:
<template>
<button @click="increment">
Count is: {{ state.count }}, double is: {{ state.double }}
</button>
</template>
<script>
import { reactive, computed } from 'vue'
export default {
setup() {
const state = reactive({
count: 0,
double: computed(() => state.count * 2)
})
function increment() {
state.count++
}
return {
state,
increment
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
<template>
<div>
<h2 ref="titleRef">{{ formattedMoney }}</h2>
<input v-model="delta" type="number">
<button @click="add">Add</button>
</div>
</template>
<script>
import { ref, computed, …Run Code Online (Sandbox Code Playgroud) 虽然Vue Composition API RFC 参考站点有很多watch模块的高级使用场景,但没有关于如何观看组件 props 的示例?
在Vue Composition API RFC 的主页或Github 中的 vuejs/composition-api 中也没有提到它。
我创建了一个Codesandbox来详细说明这个问题。
<template>
<div id="app">
<img width="25%" src="./assets/logo.png">
<br>
<p>Prop watch demo with select input using v-model:</p>
<PropWatchDemo :selected="testValue"/>
</div>
</template>
<script>
import { createComponent, onMounted, ref } from "@vue/composition-api";
import PropWatchDemo from "./components/PropWatchDemo.vue";
export default createComponent({
name: "App",
components: {
PropWatchDemo
},
setup: (props, context) => {
const testValue = ref("initial");
onMounted(() => …Run Code Online (Sandbox Code Playgroud) 如何在 Vue 3 中创建事件总线?
在 Vue 2 中,它是:
export const bus = new Vue();
Run Code Online (Sandbox Code Playgroud)
bus.$on(...)
bus.$emit(...)
Run Code Online (Sandbox Code Playgroud)
在 Vue 3 中,Vue不再是构造函数,而是Vue.createApp({});返回一个没有$on和$emit方法的对象。
我正在努力去computed工作<script setup>:
<template>
<div>
<p>{{ whatever.get() }}</p>
</div>
</template>
<script setup>
import {computed} from "vue";
const whatever = computed({
get() {
console.log('check')
return 'check?';
}
})
</script>
Run Code Online (Sandbox Code Playgroud)
该语句console.log()通过了,但return似乎抛出了一个错误:
check
Vue warn]: Unhandled error during execution of render function
at <Cms>
Uncaught TypeError: $setup.whatever.get is not a function
at Proxy.render (app.js?id=d9e007128724c77a8d69ec76c6c081a0:39550:266)
at renderComponentRoot (app.js?id=d9e007128724c77a8d69ec76c6c081a0:25902:44)
at ReactiveEffect.componentUpdateFn [as fn] (app.js?id=d9e007128724c77a8d69ec76c6c081a0:30019:57)
at ReactiveEffect.run (app.js?id=d9e007128724c77a8d69ec76c6c081a0:23830:29)
at setupRenderEffect (app.js?id=d9e007128724c77a8d69ec76c6c081a0:30145:9)
at mountComponent (app.js?id=d9e007128724c77a8d69ec76c6c081a0:29928:9)
at processComponent (app.js?id=d9e007128724c77a8d69ec76c6c081a0:29886:17)
at patch …Run Code Online (Sandbox Code Playgroud) javascript vue.js vuejs3 vue-composition-api vue-script-setup
我正在尝试使用组合 API 将数据从子级发送到父级
我收到以下警告。
[Vue 警告]:无关的非发射事件侦听器 (updatedcount) 已传递给组件,但无法自动继承,因为组件呈现片段或文本根节点。如果侦听器仅用作组件自定义事件侦听器,请使用“发射”选项声明它。at <HelloWorld onUpdatedcount=fn > at
子组件.vue
<template>
<h1>{{ store.count }}</h1>
<button @click="fired">click me</button>
</template>
<script>
import useStore from "../store/store.js";
export default {
name: "HelloWorld",
setup(_,{ emit }) {
const store = useStore();
const fired = () => {
store.count++;
emit("updatedcount", store.count);
};
return {
store,
fired
};
},
};
</script>
Run Code Online (Sandbox Code Playgroud)
父组件.vue
<template>
<div>
{{ hello }}
<br />
<br />
<input type="text" v-model="hello.searchQuery" />
<br><br>
<button @click="hello.count--">click me too!</button>
<hello-world @updatedcount="mydata" />
</div> …Run Code Online (Sandbox Code Playgroud) 我使用Vue 3.1.1
我在实验阶段使用脚本设置和单个文件组件。使用脚本设置,我了解defineProps、defineEmit和useContext,但我不了解如何使用渲染函数。
<script lang="ts" setup>
import { defineProps } from 'vue'
const props = defineProps<{
text: string
}>()
const handleClick = () => {
console.log('click!!')
}
// Render function...
/* The template I want to create.
<button
class="btn btn-primary"
type="button"
@click="handleClick"
>
{{ props.text }}
</button>
*/
</script>
Run Code Online (Sandbox Code Playgroud) javascript vue.js vuejs3 vue-composition-api vue-script-setup
我正在使用带有打字稿的 vue 组合 api。
如何使用打字稿打字系统对组件道具进行强类型输入?
我想v-model在组件上添加 a 但收到此警告:
[Vue warn]: Component emitted event "input" but it is neither declared in the emits option nor as an "onInput" prop.
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
[Vue warn]: Component emitted event "input" but it is neither declared in the emits option nor as an "onInput" prop.
Run Code Online (Sandbox Code Playgroud)
// Parent.vue
<template>
<h2>V-Model Parent</h2>
<Child v-model="name" label="Name" />
<p>{{ name }}</p>
</template>
<script setup>
import { ref } from 'vue'
import Child from './Child.vue'
const name = ref('')
</script>
Run Code Online (Sandbox Code Playgroud)
我试图重现本教程,但我被困住了,不知道我错过了什么。
{{ …
这是我的RequestList.vue组件
<template>
<ion-item v-for="request in requests" @click="showRequest(request)" :key="request.id"
text-wrap>
<ion-label>
<b>Name:</b> {{ request.event.name }}
<br>
<b>Venue:</b>{{ request.event.venue.name }}
<br>
<b>Date:</b> {{ request.event.date }}
</ion-label>
</ion-item>
</template>
<script>
import {useRouter} from 'vue-router'
import {IonItem, IonLabel} from '@ionic/vue'
import store from '@/store';
export default {
components: {
IonLabel,
IonItem
},
props: {
requests: Array
},
setup(props) {
const router = useRouter()
const showRequest= (request)=> {
console.log('request', props.request);
store.requests.setRequest(props.requests);
};
return {router, showRequest}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
我的store/modules/requests.js档案
import {computed, …Run Code Online (Sandbox Code Playgroud) 我有一个父组件,我需要调用其子组件之一中存在的方法:
<template>
<div>
<button @click="callChildMethod">
<child-component ref="child" />
</div>
</template>
<script>
setup(props) {
const child = ref(null)
const callChildMethod = () => {
child.doSomething()
}
return {
child,
callChildMethod
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
子组件包含doSomething方法:
const doSomething = () => { console.log('calling method....') }
Run Code Online (Sandbox Code Playgroud)
由于我使用的是 VueJS3 和 Composition API,因此我的方法是使用模板引用来调用子组件中的方法。显然不起作用,但我看不出我错过了什么。有人对这个有线索吗?提前致谢
javascript vue.js vuejs3 vue-composition-api vue-script-setup