【问题标题】:How to use slugify with the Vue3 Setup function? [closed]如何在 Vue3 设置功能中使用 slugify? [关闭]
【发布时间】:2021-05-20 11:53:33
【问题描述】:

我已经习惯在Vue2中使用slugify,但是在Vue3中使用Setup功能时无法让它工作。

我有一个包含播放列表的数据库,当我创建一个新的播放列表时,我想根据播放列表标题创建一个 slug。 但我收到一条错误消息,提示我“无法设置 null 的属性‘值’”,它指向 slug 值。

这样做的正确方法是什么?

 setup() {

       const { filePath, url, uploadImage } = useStorage()
       const { error, addDoc } = useCollection('playlists')

       const title = ref('')
       const description = ref('')
       const file = ref(null)
       const fileError = ref(null)
       const isPending = ref(false)
       const slug = (null)

       const handleSubmit = async () => {
          if (file.value) {
             isPending.value = true
             await uploadImage(file.value)
             await addDoc({
                title: title.value,
                description: description.value,
                slug: slug.value,
                coverUrl: url.value,
                filePath: filePath.value,
                songs: [],
                createdAt: timestamp()
             })
             isPending.value = false
             if(!error.value) {
                console.log('Playlist added')
             }
          }

          if (title.value) {
             slug.value = slugify(title.value, {
                replacement: '-',
                remove: /[*+~.()'"!:@]/g,
                lower: true
             })
          }
       }

       // allowed file types

       const types = ['image/png', 'image/jpeg']

       const handleChange = (e) => {
          const selected = e.target.files[0]
          console.log(selected)

          if (selected && types.includes(selected.type)) {
             file.value = selected
             fileError.value = null 
          } else {
             file.value = null
             fileError.value = 'Please select an image of the type JPG or PNG'
          }
       }

       return {
          title,
          description,
          handleSubmit,
          handleChange,
          fileError,
          file,
          isPending,
          slug
       }
    }

【问题讨论】:

    标签: vue.js vue-component slug vuejs3


    【解决方案1】:

    你的错误不是因为错字吗?

    const slug = (null) // => const slug = ref(null) ?
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-29
      • 2017-05-15
      • 1970-01-01
      • 2021-10-29
      • 2023-03-22
      • 2017-10-19
      • 1970-01-01
      • 2013-05-05
      相关资源
      最近更新 更多