英文:
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
<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
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: 'profile_name',
description: 'description',
});
答案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.
<template>
<form @submit.prevent="submit">
<input id="profile_name" :placeholder="$page.props.profile_name || 'profile_name'" v-model="form.profile_name" />
<input id="description" :placeholder="$page.props.description || 'description'" v-model="form.description" />
<button type="submit">Submit</button>
</form>
</template>
<script>
import { useForm } from '@inertiajs/vue3';
import { router } from '@inertiajs/vue3';
import { usePage } from '@inertiajs/vue3';
export default {
name: 'MyComponent',
setup() {
const page = usePage();
const form = useForm({
profile_name: page.props.profile_name || 'profile_name',
description: page.props.description || 'description',
});
function submit() {
router.post('/members', form);
}
return {
form,
submit,
};
},
};
</script>
英文:
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.
<template>
<form @submit.prevent="submit">
<input id="profile_name" :placeholder="$page.props.profile_name || 'profile_name'" v-model="form.profile_name" />
<input id="description" :placeholder="$page.props.description || 'description'" v-model="form.description" />
<button type="submit">Submit</button>
</form>
</template>
<script>
import { useForm } from '@inertiajs/vue3';
import { router } from '@inertiajs/vue3';
import { usePage } from '@inertiajs/vue3';
export default {
name: 'MyComponent',
setup() {
const page = usePage();
const form = useForm({
profile_name: page.props.profile_name || 'profile_name',
description: page.props.description || 'description',
});
function submit() {
router.post('/members', form);
}
return {
form,
submit,
};
},
};
</script>
答案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:
<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>
I didn't get the submit()
method at the moment.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论