首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vue ReferenceError:未定义slugify

Vue ReferenceError:未定义slugify
EN

Stack Overflow用户
提问于 2020-08-15 11:31:20
回答 1查看 265关注 0票数 0

因此,我正在尝试从一个输入中获取值,并将其转换为段塞,以便在另一个输入上显示。我使用的是Laravel Spark、Vue和Bootstrap 4。

到目前为止,我在listings.blade.php中有以下内容

代码语言:javascript
复制
<createlisting inline-template>
  <div class="container">
    
  <h1>
    Create a listing
  </h1>
  <form class="form">
      <div class="form-group">
    <label for="name">Name</label>
    <input type="text" class="form-control" v-on:keyup="listingslug" id="name" name="name" placeholder="Example input placeholder">
  </div>
      <label for="slug">Your vanity URL</label>
<div class="input-group mb-3">
  <div class="input-group-prepend">
    <span class="input-group-text" id="basic-addon3">{{ env('APP_URL') }}/listing/</span>
  </div>
  <input type="text" class="form-control" id="slug" name="slug" aria-describedby="basic-addon3">
</div>
  </form>
</div>
</createlisting>

我在一个createlisting.js文件中有这个文件

代码语言:javascript
复制
Vue.component('createlisting', {
    data() {
        return {
            form: new SparkForm({
                name: '',
                description: ''
            })
        };
    },
    methods: {
      slugify: function(text) {
        return text
          .toString()                     // Cast to string
          .toLowerCase()                  // Convert the string to lowercase letters
          .normalize('NFD')       // The normalize() method returns the Unicode Normalization Form of a given string.
          .trim()                         // Remove whitespace from both sides of a string
          .replace(/\s+/g, '-')           // Replace spaces with -
         .replace(/[^\w\-]+/g, '')       // Remove all non-word chars
          .replace(/\-\-+/g, '-');        // Replace multiple - with single -
      },
      listingslug: function(text) {
        document.getElementById("slug").value = this.slugify(text); 
      }
    }
});

我将custom.js文件中的slugify函数(如下所示)添加到我的Vue组件网中,看看这是否有帮助。

代码语言:javascript
复制
/**
*   This is the slugify function, to allow us to slugify text
*/
function slugify(text) {
  return text
    .toString()                     // Cast to string
    .toLowerCase()                  // Convert the string to lowercase letters
    .normalize('NFD')       // The normalize() method returns the Unicode Normalization Form of a given string.
    .trim()                         // Remove whitespace from both sides of a string
    .replace(/\s+/g, '-')           // Replace spaces with -
    .replace(/[^\w\-]+/g, '')       // Remove all non-word chars
    .replace(/\-\-+/g, '-');        // Replace multiple - with single -
}

当涉及到Vue时,我是一个新手,在Javascript方面,我仍然是一个相当初级的人。我做错了什么?

另一个部分是将vue模板中的slugify(text)更改为this.slugify(text),然后输出为"object-keyboardevent“。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-15 14:48:10

我认为这里的部分问题是,对于您输入的文本,您想要'slugified',输入的数据没有正确地绑定到'name‘值(至少不是以'Vuey’的方式)。

您可以通过使用v-model绑定它来实现,如下所示:

代码语言:javascript
复制
//note that I removed your v-on:keyup, and placeholder="Example input placeholder" for simplicity/demo purposes

<input type="text" class="form-control" id="name" name="name" v-model="name">

请参阅关于v-model/Form输入绑定的Vue文档:https://vuejs.org/v2/guide/forms.html

你可以考虑使用一个用@click监听点击的按钮,而不是使用快捷键。然后你可以通过点击来调用你的方法'listingslug‘。

所以它看起来就像这样:

代码语言:javascript
复制
<button @click="listingslug">Slugify</button>

有关活动的更多详细信息,请参阅有关主题的官方Vue文档:https://vuejs.org/v2/guide/events.html

然后您可以将'slugified‘输出输入值设置为数据值,如下所示:

代码语言:javascript
复制
<input type="text" class="form-control" id="slug" name="slug" value="{{slugified}}" aria-describedby="basic-addon3">

但要让它起作用,您还需要将'slugified‘添加到您的data属性中,并将其声明为一个空字符串或null值(就像您已经对name data属性所做的那样)。

这将更改listingslug方法,使其如下所示:

代码语言:javascript
复制
listingslug: function() {
        this.slugified = this.slugify(this.name); 
      }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63422308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档