Simple component v-model not updating.

huangapple go评论54阅读模式
英文:

Simple component v-model not updating

问题

以下是您要翻译的内容:

"So I'm finally trying to get my head into the Laravel using Vue scene.

For the life of me, I can't work out why this simple component v-model doesn't seem to be updating the on page content.

<template>
    <div class="row">
        <div class="col-12">
            <div id="card" class="card">
                <div class="card-header">
                    <h4>{{ header }}</h4>
                </div>
                <div class="card-body">
                    <input v-model="newItem" type="text" placeholder="title">
                    <hr>
                    ( {{ newItem }} )
                    <ul>
                        <li v-for="item in items" :key="item.id">{{ item.label }}</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "add-category",
        data() {
            return {
                header: 'Add Category',
                newItem: '',
                items: [
                    { id: 1, label: 'TESTING 123' },
                    { id: 2, label: 'TESTING ABC' },
                ]
            }
        },
        methods: {

        }
    }
</script>

Simple component v-model not updating.

No matter what text is added in the input, the content between the ( ) never updates and remains blank?

I was following this vue tutorial video here: https://vueschool.io/lessons/user-inputs-vue-devtools-in-vue-3?friend=vuejs

From what I can tell, it should be working?

Could this be a conflict related to Laravel potentially?

Any thoughts are much appreciated."

英文:

So I'm finally trying to get my head into the Laravel using Vue scene.

For the life of me, I can't work out why this simple component v-model doesn't seem to be updating the on page content.

&lt;template&gt;
    &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;col-12&quot;&gt;
            &lt;div id=&quot;card&quot; class=&quot;card&quot;&gt;
                &lt;div class=&quot;card-header&quot;&gt;
                    &lt;h4&gt;{{ header }}&lt;/h4&gt;
                &lt;/div&gt;
                &lt;div class=&quot;card-body&quot;&gt;
                    &lt;input v-model=&quot;newItem&quot; type=&quot;text&quot; placeholder=&quot;title&quot;&gt;
                    &lt;hr&gt;
                    ( {{ newItem }} )
                    &lt;ul&gt;
                        &lt;li v-for=&quot;item in items&quot; :key=&quot;item.id&quot;&gt;{{ item.label }}&lt;/li&gt;
                    &lt;/ul&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
    export default {
        name:&quot;add-category&quot;,
        data(){
            return {
                header: &#39;Add Category&#39;,
                newItem: &#39;&#39;,
                items: [
                    {id: 1, label:&#39;TESTING 123&#39;},
                    {id: 2, label:&#39;TESTING ABC&#39;},
                ]
            }
        },
        methods:{

        }
    }
&lt;/script&gt;

Simple component v-model not updating.

No matter what text is added in the input, the content between the ( ) never updates and remains blank?

I was following this vue tutorial video here:https://vueschool.io/lessons/user-inputs-vue-devtools-in-vue-3?friend=vuejs

From what I can tell, it should be working?

Could this be a conflict related to Laravel potentially?

Any thoughts are much appreciated.

答案1

得分: 0

如此看来,我的问题似乎出在我如何设置 app.js 上,或者说我应该说没有设置,因为它似乎并没有通过 Laravel 使用 Vue,而是通过主 HTML 文件上的一个包含。

所以我移除了那些包含,并将我的 app.js 更改为以下内容,一切都按预期开始工作了!

require('./bootstrap')

// 1. 定义路由组件。
const Home = { template: '<div>Home</div>' }
const About = { template: '<div>About</div>' }

import Welcome from './components/App.vue';
import CategoryList from './components/category/List.vue';
import CategoryCreate from './components/category/Add.vue';
import CategoryEdit from './components/category/Edit.vue';

// 2. 定义一些路由
const routes = [
    { path: '/', component: Home },
    { path: '/about', component: About },
    { path: '/welcome', component: Welcome },
    { path: '/category', component: CategoryList },
    { path: '/category/:id/edit', component: CategoryEdit },
    { path: '/category/add', component: CategoryCreate },
]

window.axios = require("axios");

import { createRouter, createWebHistory } from 'vue-router';
const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

window.Vue = require("Vue");

const app = Vue.createApp({})
app.use(router)
app.mount('#app')

所以现在我的 v-model 更新如预期一样!

英文:

So it seems like my issue was with how I had my app.js setup, or should I say wasn't setup as it didn't seem to be using vue via Laravel rather via an include on the main html file.

So I have removed those includes and changed my app.js to the following and it all started working as expected!

require(&#39;./bootstrap&#39;)

// 1. Define route components.
const Home = { template: &#39;&lt;div&gt;Home&lt;/div&gt;&#39; }
const About = { template: &#39;&lt;div&gt;About&lt;/div&gt;&#39; }

import Welcome from &#39;./components/App.vue&#39;;
import CategoryList from &#39;./components/category/List.vue&#39;;
import CategoryCreate from &#39;./components/category/Add.vue&#39;;
import CategoryEdit from &#39;./components/category/Edit.vue&#39;;

// 2. Define some routes
const routes = [
    { path: &#39;/&#39;, component: Home },
    { path: &#39;/about&#39;, component: About },
    { path: &#39;/welcome&#39;, component: Welcome },
    { path: &#39;/category&#39;, component: CategoryList },
    { path: &#39;/category/:id/edit&#39;, component: CategoryEdit },
    { path: &#39;/category/add&#39;, component: CategoryCreate },
]

window.axios = require(&quot;axios&quot;);

import { createRouter,createWebHistory} from &#39;vue-router&#39;
const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

window.Vue = require(&quot;Vue&quot;);

const app = Vue.createApp({})
app.use(router)
app.mount(&#39;#app&#39;)

So now my v-model updates as expected!

Simple component v-model not updating.

huangapple
  • 本文由 发表于 2023年5月21日 23:02:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定