Uncaught (in promise) ReferenceError: $page is not defined

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

Uncaught (in promise) ReferenceError: $page is not defined

问题

以下是您要翻译的内容:

What I want to achieve

我有一个使用 Laravel + InertiaJS/VueJS 3 的项目。在项目中,有一个编辑表单,如果 $page.props.profile_name 的值不为空,应该显示其内容,否则显示字符串 profile_name

The bug message from the browser's console

浏览器控制台中的错误消息:

Uncaught (in promise) ReferenceError: $page is not defined

当我看到这个消息时,我的组件内容不显示在界面上。

My VueJS 3 Code

我的 VueJS 3 代码:

<template>
    <form @submit.prevent="submit">
        <input id="profile_name" v-model="form.profile_name" />

        <input id="description" v-model="form.description" />

        <button type="submit">Submit</button>
    </form>
</template>

<script setup>
    import { useForm } from '@inertiajs/vue3';
    import { router } from '@inertiajs/vue3';

    const form = useForm({
        profile_name: $page.props.profile_name || 'profile_name',
        description: $page.props.description || 'description',
    });

    function submit() {
        router.post('/members', form);
    }
</script>

How I can solve the issue for now

目前我可以如何解决这个问题:

代码运行正常,但是使用硬编码的内容。如果我用下面的方式替换表单常量,那么问题就会解决。我希望一旦 $page.props.profile_name 不为空,就能从中获取内容。

const form = useForm({
    profile_name: 'profile_name',
    description: 'description',
});
英文:

What I want to achieve

I have a project using Laravel + InertiaJS/VueJS 3. In it, there is an edit form that should display the content of $page.props.profile_name if the value is not empty or the string profile_name if the value is non-existent.

The bug message from the browser's console

> Uncaught (in promise) ReferenceError: $page is not defined

When I see this message, the content of my component is not visible in the interface.

My VueJS 3 Code

&lt;template&gt;
    &lt;form @submit.prevent=&quot;submit&quot;&gt;
        &lt;input id=&quot;profile_name&quot; v-model=&quot;form.profile_name&quot; /&gt;

        &lt;input id=&quot;description&quot; v-model=&quot;form.description&quot; /&gt;

        &lt;button type=&quot;submit&quot;&gt;Submit&lt;/button&gt;
    &lt;/form&gt;
&lt;/template&gt;

&lt;script setup&gt;
    import { useForm } from &#39;@inertiajs/vue3&#39;;
    import { router } from &#39;@inertiajs/vue3&#39;;

    const form = useForm({
        profile_name: $page.props.profile_name || &#39;profile_name&#39;,
        description: $page.props.description || &#39;description&#39;,
    });

    function submit() {
        router.post(&#39;/members&#39;, form);
    }
&lt;/script&gt;

How I can solve the issue for now

The code works fine, but with hard-coded content, if I replace the form constant this way. I would prefer to get my content from $page.props.profile_name as soon as $page.props.profile_name is not empty.

const form = useForm({
    profile_name: &#39;profile_name&#39;,
    description: &#39;description&#39;,
});

答案1

得分: 2

I originally posted the answer within the question, but it was removed by a proofreader. Credits to yoduh for his answer. And thanks to efecdml for taking part in the discussion.

&lt;template&gt;
    &lt;form @submit.prevent=&quot;submit&quot;&gt;
        &lt;input id=&quot;profile_name&quot; :placeholder=&quot;$page.props.profile_name || &#39;profile_name&#39;&quot; v-model=&quot;form.profile_name&quot; /&gt;

        &lt;input id=&quot;description&quot; :placeholder=&quot;$page.props.description || &#39;description&#39;&quot; v-model=&quot;form.description&quot; /&gt;

        &lt;button type=&quot;submit&quot;&gt;Submit&lt;/button&gt;
    &lt;/form&gt;
&lt;/template&gt;

&lt;script&gt;
    import { useForm } from &#39;@inertiajs/vue3&#39;;
    import { router } from &#39;@inertiajs/vue3&#39;;
    import { usePage } from &#39;@inertiajs/vue3&#39;;

    export default {
        name: &#39;MyComponent&#39;,
        setup() {
            const page = usePage();

            const form = useForm({
                profile_name: page.props.profile_name || &#39;profile_name&#39;,
                description: page.props.description || &#39;description&#39;,
            });

            function submit() {
                router.post(&#39;/members&#39;, form);
            }

            return {
                form,
                submit,
            };
        },
    };
&lt;/script&gt;
英文:

I originally posted the answer within the question, but it was removed by a proofreader. Credits to yoduh for his answer. And thanks to efecdml for taking part in the discussion.

&lt;template&gt;
    &lt;form @submit.prevent=&quot;submit&quot;&gt;
        &lt;input id=&quot;profile_name&quot; :placeholder=&quot;$page.props.profile_name || &#39;profile_name&#39;&quot; v-model=&quot;form.profile_name&quot; /&gt;

        &lt;input id=&quot;description&quot; :placeholder=&quot;$page.props.description || &#39;description&#39;&quot; v-model=&quot;form.description&quot; /&gt;

        &lt;button type=&quot;submit&quot;&gt;Submit&lt;/button&gt;
    &lt;/form&gt;
&lt;/template&gt;

&lt;script&gt;
    import { useForm } from &#39;@inertiajs/vue3&#39;;
    import { router } from &#39;@inertiajs/vue3&#39;;
    import { usePage } from &#39;@inertiajs/vue3&#39;;

    export default {
        name: &#39;MyComponent&#39;,
        setup() {
            const page = usePage();

            const form = useForm({
                profile_name: page.props.profile_name || &#39;profile_name&#39;,
                description: page.props.description || &#39;description&#39;,
            });

            function submit() {
                router.post(&#39;/members&#39;, form);
            }

            return {
                form,
                submit,
            };
        },
    };
&lt;/script&gt;

答案2

得分: 0

我知道像从DOM引用元素来设置的方式在Composition API中不是这样的,我没有完全理解这段代码的用意。虽然我现在想到的是这种方法:

<template>
  <form @submit.prevent="submit">
    <input id="profile_name" v-model="form.profile_name"/>

    <input id="description" v-model="form.description"/>

    <button type="submit">Submit</button>
  </form>
</template>

<script setup>
import { router } from '@inertiajs/vue3';
import { ref } from "vue";

const form = ref({
  "profile_name": "",
  "description": ""
})

function submit() {
  router.post('/members', form);
}
</script>

我暂时不理解submit()方法的用途。

英文:

I know like referencing elements from DOM to setup is not like that in Composition API and didn't get the whole point of this code. Though this is the method comes to my mind right now:

&lt;template&gt;
  &lt;form @submit.prevent=&quot;submit&quot;&gt;
    &lt;input id=&quot;profile_name&quot; v-model=&quot;form.profile_name&quot;/&gt;

    &lt;input id=&quot;description&quot; v-model=&quot;form.description&quot;/&gt;

    &lt;button type=&quot;submit&quot;&gt;Submit&lt;/button&gt;
  &lt;/form&gt;
&lt;/template&gt;

&lt;script setup&gt;
import { router } from &#39;@inertiajs/vue3&#39;;
import {ref} from &quot;vue&quot;;

const form = ref({
  &quot;profile_name&quot;:&quot;&quot;,
  &quot;description&quot;:&quot;&quot;
})

function submit() {
  router.post(&#39;/members&#39;, form);
}
&lt;/script&gt;

I didn't get the submit() method at the moment.

huangapple
  • 本文由 发表于 2023年6月12日 02:07:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451869.html
匿名

发表评论

匿名网友

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

确定