【发布时间】:2020-12-04 21:42:59
【问题描述】:
所以我试图从一个输入中获取值,并将其变成一个 slug,以显示在另一个输入上。我正在使用 Laravel Spark、Vue 和 Bootstrap 4。
到目前为止,我在listings.blade.php 中有这个作为我的内容
<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 文件中有这个
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 组件中,看看是否有帮助。
/**
* 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”。
【问题讨论】:
-
要从
methods引用函数,请使用this.FUNCTION_NAME(即this.slugify(text))。对于从custom.js导入的函数,我们需要查看函数是如何导出的(你使用export default吗?),以及你是如何导入的。 -
我实际上只是做了
this.slugify(text),显示的输出是object-keyboardevent。那是它被输出到的地方。
标签: javascript laravel twitter-bootstrap vue.js laravel-spark