小编wit*_*ein的帖子

如何在 vue 3 脚本设置中的组件上使用 v-model

我想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)

我试图重现本教程,但我被困住了,不知道我错过了什么。

{{ …

vue.js v-model vuejs3 vue-composition-api vue-script-setup

25
推荐指数
2
解决办法
4万
查看次数

Vue3 TypeError:模板 ref.value 为 null

如何清除控制台中的以下错误:

在此输入图像描述

TypeError: ref.value is null

该错误仅在调整大小事件时出现。每次调整窗口大小时,都会渲染图表。于是错误信息一次又一次的出现。文档显示模板引用也用空值初始化(Source)进行初始化。所以初始化后我必须做一些事情,对吗?

这是我的代码:

<template>
  <canvas
    ref="chartRef"
  />
</template>
Run Code Online (Sandbox Code Playgroud)
<script setup>
// ...
// on resize
export const chartRef = ref(null)
export function createChart () {
  const ctx = chartRef.value.getContext('2d')
  if (ctx !== null) { // fix me
    getDimensions()
    drawChart(ctx)
  }
}
// ...
</script>

Run Code Online (Sandbox Code Playgroud)

如何清理我的控制台以便不再出现错误消息?谢谢。

templates canvas refs vuejs3 vue-composition-api

18
推荐指数
3
解决办法
2万
查看次数

vue3中如何强制vite清除缓存

我有一个副业项目,Vue.js 3vite作为我的捆绑商。

每次构建之后,捆绑的文件都会从之前的构建中获得相同的哈希值,例如:

index.432c7f2f.js   <-- the hash will be identical after each new build
index.877e2b8d.css
vendor.67f46a28.js
Run Code Online (Sandbox Code Playgroud)

因此,在每次新构建之后(文件上具有相同的哈希值),我必须重新加载浏览器以清除缓存并查看我所做的更改。

我尝试在 中使用不同的版本号强制清除package.json,但是:

  1. 在Vite/Rollup环境下不起作用,
  2. 每次更改后都手动输入新号码是没有意义的。

问题:

有没有办法配置 vite 在新构建后随机创建新哈希,或者您知道清除缓存的另一个技巧吗?

caching bundle rollup vuejs3 vite

15
推荐指数
2
解决办法
5万
查看次数

使用 vite 和 vue3 构建过程后资产未显示

运行时,npm run build我的图片src/assets/...在 dist 目录/生产版本中不可用。所以网站上没有显示。在开发模式下它肯定可以工作。

有什么想法如何让它们在构建后可用吗?

vue.js vuejs3 vite

10
推荐指数
1
解决办法
2万
查看次数

如何将可组合项导入 Nuxt 3 中的服务器/api

我刚刚统一了一个useSupabase可组合项,以删除重复的代码并访问 supabase 上下文。

但是当我将可组合项导入 server/api 文件夹时出现错误:

消息“useSupabase 未定义”

// composables/useSupabase.js
import { createClient } from '@supabase/supabase-js'
const useSupabase = () => {
  const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)
  return {
    supabase
  }
}
export default useSupabase
Run Code Online (Sandbox Code Playgroud)
// server/api/login.js
export default async (event) => {
  const body = await useBody(event)
  const { supabase } = useSupabase()

  const { user } = await supabase.auth.signUp({
    email: body.email,
    password: body.password
  })
  return user
}

Run Code Online (Sandbox Code Playgroud)

有没有办法将可组合项导入端点?

另一次尝试

// server/api/logout.js
import { supabase } from …
Run Code Online (Sandbox Code Playgroud)

javascript nuxtjs3

8
推荐指数
1
解决办法
7590
查看次数

如何正确删除 vue 3 中的事件监听器

我有一个事件侦听器,可以在安装项目时以及每次调整大小事件之后获取视口尺寸。

我不知道如何正确删除事件侦听器。

const { createApp, onMounted, ref } = Vue;
const app = createApp({
  setup() {
    const dim = ref({})
    
    onMounted(() => {
      dim.value = getDimensions()
      // adding the event listener...
      window.addEventListener('resize', debounce(() => {
        // ...but how to remove the event listener correctly?
        console.log('resize')
        dim.value = getDimensions()
      }, 250))
    })
    
    function getDimensions () {
      return {
        w: window.innerWidth,
        h: window.innerHeight
      }
    }
    
    // generic debounce function
    function debounce (func, wait) {
    let timeout
    return function executedFunction (...args) {
      const …
Run Code Online (Sandbox Code Playgroud)

event-listener vue.js removeeventlistener vuejs3 vue-composition-api

7
推荐指数
2
解决办法
9433
查看次数

如何在 Nuxt 3 服务器/api 中传递参数?

我不知道如何将参数传递给 Nuxt 3 中的匿名函数。

index.vue:

<template>
  <form @submit.prevent="signUpNewsletter()">
    <input type="email" placeholder="example@x.com" v-model="userEmail">
    <input type="submit" value="Submit">
  </form>
</template>

<script setup>
const userEmail = ref('x@x.de')

function signUpNewsletter () {
  useAsyncData(
    'newsletter',
    () => $fetch('/api/sign_up_news', {
      method: 'POST', // Post method works
      body: {
        email: userEmail.value
      }
    })
  )
}
</script>
Run Code Online (Sandbox Code Playgroud)

server/api/sign_up_news.js:

import { createClient } from '@supabase/supabase-js'

export default async (email) => { // can't read the parameter
  const SUPABASE_KEY = 'key123'
  const SUPABASE_URL = 'url.supabase.co'
  const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)

  const …
Run Code Online (Sandbox Code Playgroud)

javascript anonymous-function vuejs3 supabase nuxtjs3

7
推荐指数
1
解决办法
1万
查看次数

在vue 3中观察父组件的子属性

我想知道如何使用组合 API 观察 Vue 3 中父组件的子属性(我正在使用实验脚本设置)。

<template>//Child.vue
  <button 
    @click="count++" 
    v-text="'count: ' + count" 
  />
</template>

<script setup>
import { ref } from 'vue'

let count = ref(1)
</script>
Run Code Online (Sandbox Code Playgroud)
<template>//Parent.vue
  <p>parent: {{ count }}</p> //update me with a watcher
  <Child ref="childComponent" />
</template>


<script setup>
import Child from './Child.vue'
import { onMounted, ref, watch } from 'vue'

const childComponent = ref(null)
let count = ref(0)

onMounted(() => {
  watch(childComponent.count.value, (newVal, oldVal) => {
    console.log(newVal, oldVal);
    count.value = newVal
  }) …
Run Code Online (Sandbox Code Playgroud)

vue.js vue-component vuejs3 vue-composition-api vue-script-setup

6
推荐指数
1
解决办法
5438
查看次数

如何在 vue 3 脚本设置中使用 &lt;component :is=""&gt;

我正在使用实验脚本设置来创建学习环境。我有一个自制的导航栏,打开一个组件。

我在使用该<component :is="" />方法时遇到问题。此方法在组件基础 -> 动态组件下的文档中进行了描述

在 Vue 3 Composition API 中,它按预期工作:

<template>
  <NavigationBar
    @switchTab="changeTab"
    :activeTab="tab"
  />
  <component :is="tab" />
</template>

<script>
import { ref } from 'vue'
import NavigationBar from './components/NavigationBar.vue'
import TemplateSyntax from './components/TemplateSyntax.vue'
import DataPropsAndMethods from './components/DataPropsAndMethods.vue'

export default {
  components: {
    NavigationBar,
    TemplateSyntax,
    DataPropsAndMethods
  },
  setup () {
    const tab = ref('DataPropsAndMethods')
    function changeTab (newTab) {
      tab.value = newTab
    }

    return {
      changeTab,
      tab
    }
  }
}
</script>
Run Code Online (Sandbox Code Playgroud)

我的script …

javascript vue.js vuejs3 vue-composition-api vue-script-setup

5
推荐指数
1
解决办法
787
查看次数

无法在 vue 3 组合 api 中的组件上使用模板引用

我想从父级获取 vue.js 组件的尺寸(我正在使用实验性脚本设置)。

当我在组件内使用 ref 时,它按预期工作。我得到尺寸:

// Child.vue
<template>
  <div ref="wrapper">
   // content ...
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'

const wrapper = ref(null)

onMounted(() => {
  const rect = wrapper.value.getBoundingClientRect()
  console.log(rect) // works fine!
})
</script>
Run Code Online (Sandbox Code Playgroud)

但我想获取父组件内的维度。这可能吗?

我试过这个:

// Parent.vue
<template>
  <Child ref="wrapper" />
</template>
<script setup>
import Child from './Child'
import { ref, onMounted } from 'vue'

const wrapper = ref(null)

onMounted(() => {
  const rect = wrapper.value.getBoundingClientRect()
  console.log(rect) // failed! …
Run Code Online (Sandbox Code Playgroud)

ref vue.js vue-component vuejs3 vue-composition-api

5
推荐指数
3
解决办法
2821
查看次数

如何通过 vue-svg-loader 在 vue.js 中导入多个 svg

我想在一个 vue 组件中导入多个 svg。文档说我必须导入它们中的每一个,但是如何以更短更清晰的方式导入多个 svg?

vue-svg-loader 文档:https : //vue-svg-loader.js.org/

<script>
import Info from "@/assets/svgs/info.svg";
import Help from "@/assets/svgs/help.svg";
import Close from "@/assets/svgs/close.svg";
// etc. pp.

export default {
  components: {
    Info,
    Help,
    Close
  }
</script>
Run Code Online (Sandbox Code Playgroud)

如果我想导入超过一百个 svg 会发生什么?

有什么想法可以解决这个问题吗?

javascript import svg vue.js vue-component

4
推荐指数
1
解决办法
2012
查看次数