我在使用组合 API 在 vue 3 中双向绑定反应式组件时遇到问题。
设置:
父调用代码是:
<template>
<h1>{{ message.test }}</h1>
<Message v-model="message" />
</template>
<script>
import Message from '@/components/Message.vue';
import { reactive } from 'vue';
export default {
name: 'Home',
components: { Message },
setup() {
const message = reactive({ test: '123' });
return {
message
};
}
};
</script>
Run Code Online (Sandbox Code Playgroud)
子组件代码为:
<template>
<label>
<input v-model="message" type="text" />
</label>
</template>
<script>
import { computed } from 'vue';
export default {
props: {
messageObj: {
type: Object,
default: () => …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用组合 API 在 Vue 3 组件中输入提示我的道具。
所以,我这样做:
<script lang="ts">
import FlashInterface from '@/interfaces/FlashInterface';
import { ref } from 'vue';
import { useStore } from 'vuex';
export default {
props: {
message: {
type: FlashInterface,
required: true
}
},
setup(props): Record<string, unknown> {
// Stuff
}
};
Run Code Online (Sandbox Code Playgroud)
我的FlashInterface看起来像这样:
export default interface FlashInterface {
level: string,
message: string,
id?: string
}
Run Code Online (Sandbox Code Playgroud)
除了在这种情况下我收到此错误外,此界面运行良好:
ERROR in src/components/Flash.vue:20:10
TS2693: 'FlashInterface' only refers to a type, but is being used as a value here. …Run Code Online (Sandbox Code Playgroud) 通过选项 API,我们可以使用这些方式来扩展组件。假设我们有 2 个组件。
组件1.vue
<script>
export default {
methods: {
init(message) {
alert(message)
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
Component2.vue
<script>
import Component1 from './Component1'
export default {
extends: Component1,
methods: {
showAlert() {
// we can access Component1 'init()' method here
this.init('Hello World!')
}
},
mounted() {
this.showAlert()
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
现在,如何让它与组合 API 一起工作?我已经检查过该extends属性在文档中仍然可用,但没有明确的使用说明。
https://v3.vuejs.org/api/options-composition.html#extends
考虑以下带有组合 API 的代码。
组件1.vue
<script>
import { defineComponent, ref } from 'vue'
export default defineComponent({
setup () {
const init = (message) => …Run Code Online (Sandbox Code Playgroud) 运行我的应用程序时出现此错误:
警告编译了 1 个警告 11:50:40 PM 在 ./src/store/store.js 中警告“在‘vuex’中找不到导出‘createStore’
我安装了 vuex npm install --save vuex
我正在使用 vue 3
我的 Store.js:
import { createStore } from "vuex";
import Movie from './Modules/Movie'
import post from './Modules/post'
const store = createStore({
modules: {
post,
Movie
},
});
export default store;
Run Code Online (Sandbox Code Playgroud)
我的 main.js:
import { createApp } from 'vue';
import App from './App.vue';
import router from './router'
import store from './store/store.js'
const app = createApp(App);
app.use(store);
app.use(router);
app.mount('#app');
Run Code Online (Sandbox Code Playgroud) \xe2\x9c\x85 Vue 2: \n计算属性在 vuex 存储突变后更新。
\n\xe2\x9d\x8c Vue 3: \nvuex 存储突变后计算属性不会更新。
\n目的:从 Firestore 获取帖子。添加到“postsArray”。进行突变。
\n注意:函数“getNextPosts”是从允许无限滚动的(工作)交叉观察器调用的。
\nconst postsArray: Array<string> = [];\nconst vuexStore = createStore({\n state: {\n posts: []\n },\n actions: {\n // See https://firebase.google.com/docs/firestore/query-data/query-cursors#paginate_a_query\n getFirstPosts({ commit }) {\n const getFirstPosts = async () => {\n const firstPostsQuery = firestore.collection("posts").limit(3);\n\n // Get FIRST posts.\n const firstPosts = await firstPostsQuery.get();\n\n // Add to postsArray.\n for (const doc of firstPosts.docs) {\n …Run Code Online (Sandbox Code Playgroud) 我想使用 vue.js 版本 3 上传文件。
我可以导入ref,但不确定如何使用它来获取文件数据?
文件上传测试.vue
<template>
<h1>File Upload</h1>
<div class="container">
<div>
<label>File
<input type="file" id="file" ref="file" v-on:change="onChangeFileUpload()"/>
</label>
<button v-on:click="submitForm()">Upload</button>
</div>
</div>
</template>
<script src="./FileUploadTest.ts" lang="ts"></script>
Run Code Online (Sandbox Code Playgroud)
文件上传测试.ts
import { Options, Vue } from "vue-class-component";
import { ref } from 'vue';
import axios from "../../axios/index";
@Options({})
export default class FileUploadTest extends Vue {
protected file: any;
submitForm() {
const formData = new FormData();
formData.append('bytes', this.file);
axios.post('https://localhost:44313/api/app/file/save',
formData,
{
headers: {
'Content-Type': 'multipart/form-data'
}
}
).then(function (data) { …Run Code Online (Sandbox Code Playgroud) vue.config.js我通过如下更改使用 Vue CLI 和 Vue 2 构建了一个多页面应用程序:
pages: {
index: {
entry: './src/pages/index/main.js',
template: 'public/index.html',
title: 'index page',
chunks: ['chunk-vendors', 'chunk-common', 'index']
},
admin: {
entry: './src/pages/admin/main.js',
template: 'public/index.html',
title: 'admin page',
chunks: ['chunk-vendors', 'chunk-common', 'admin']
}
},
...
Run Code Online (Sandbox Code Playgroud)
但是如何使用 Vite 和 Vue 3 构建多页面应用程序呢?
这是我的目录结构。我像这样编辑了 vite.config.js:
import vue from '@vitejs/plugin-vue'
const { resolve } = require('path')
/**
* @type {import('vite').UserConfig}
*/
export default {
plugins: [vue()],
build:{
rollupOptions:{
input:{
main:resolve(__dirname,'index.html'),
admin:resolve(__dirname,'src/admin/index.html')
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但当我构建时它返回错误,并且我无法通过 localhost:3000/admin/index.html 打开管理页面。
现在我正在为我的vue SFC 应用程序使用vite 构建工具。我通过以下链接阅读了 vite 的文档:
如果我没记错的话, config中的define选项可以用于定义全局常量。我想要做的是在此选项内的变量中定义我的应用程序的名称,然后在我的 Vue 组件中使用它。但不幸的是,文档中没有关于此选项的代码示例。
我在vite.config.js文件中尝试了以下代码:
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
define: {
global: {
appName: "my-custom-name"
}
},
plugins: [vue()]
})Run Code Online (Sandbox Code Playgroud)
我不确定语法和代码是否正确!而且如果它是正确的,我不知道如何在我的 vue 应用程序组件(.vue 文件)中调用(使用)这个常量。例如,我想在此组件的模板或脚本部分中使用它:
<template>
<div class="bgNow">
<p class="color1">
{{ use here }}
</p>
</template>
<script>
export default {
data() {
return {
name: use here …Run Code Online (Sandbox Code Playgroud)我在一个文件组件中定义了一种类型,我想在另一个组件中使用它。然而,当我在那里导入该类型时,Typescript 声称该类型未导出,并且 ESLint 会对其进行处理any。因此,类似的规则@typescript-eslint/no-unsafe-assignment就会被触发。我发现的一种解决方法是创建一个.d.ts与组件具有相同基本文件名的定义文件,但我希望我的组件保留为单个文件。
最小(非)工作示例:
<template>
<div />
</template>
<script lang="ts">
import { defineComponent } from 'vue'
export type foo = { bar: () => void }
export default defineComponent({
name: 'ExportingComponent'
})
</script>
<style></style>
Run Code Online (Sandbox Code Playgroud)
<template>
<div />
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import { foo } from './ExportingComponent'
export default defineComponent({
name: 'ImportingComponent',
setup () {
// next line triggers @typescript-eslint/no-unsafe-assignment
const fooFoo = {} as Partial<foo> …Run Code Online (Sandbox Code Playgroud) 我正在尝试访问 vue 3 中 setup() 内的数据属性,但给了我这个错误
TypeError: Cannot read property '$localStorage' of undefined
Run Code Online (Sandbox Code Playgroud)
data()
data() {
return {
$localStorage: {},
}
Run Code Online (Sandbox Code Playgroud)
设置
setup() {
this.$localStorage
}
Run Code Online (Sandbox Code Playgroud)
我如何在 setup() 中访问此属性?
vue.js ×10
vuejs3 ×10
typescript ×3
vuex4 ×3
vite ×2
vuex ×2
eslint ×1
firebase ×1
javascript ×1