gpt4 book ai didi

javascript - 在 laravel 6.x 中渲染多个 vue 组件

转载 作者:行者123 更新时间:2023-12-02 21:42:09 25 4
gpt4 key购买 nike

我是 Laravel 和 Vue 的新手,我创建了一个导航组件和一个文章组件。

我相信我已经正确设置了 Vue,问题是我的导航 vue 组件正在显示,但没有显示我的文章组件。

这是我的代码:

app.js

require("./bootstrap");
window.Vue = require("vue");

/**
* The following block of code may be used to automatically register your
* Vue components. It will recursively scan this directory for the Vue
* components and automatically register them with their "basename".
*
* Eg. ./components/ExampleComponent.vue -> <example-component></example-component>
*/

const files = require.context("./", true, /\.vue$/i);
files.keys().map(key =>
Vue.component(
key
.split("/")
.pop()
.split(".")[0],
files(key).default
)
);

const app = new Vue({
el: "#app"
});

app.blade.php

<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ config('app.name', 'Laravel') }}</title>

<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">

<!-- Fonts -->
<link rel="dns-prefetch" href="//fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
</head>
<body>
<div id="app">
<navigation />

<main class="container">
@yield('content')
</main>
</div>

<!-- Scripts -->
<script src="{{ asset('js/app.js') }}" defer></script>
</body>
</html>

欢迎.blade.php

@extends('layouts.app')

@section('content')
<articles></articles>
@endsection

我的导航显示了,但没有显示我的文章组件,一切都已成功编译npm run watch,我还重新启动了服务器。

--

以下是 vue 组件:

导航.vue

<template>
<nav class="flex items-center justify-between flex-wrap bg-teal-500 p-6">
<div class="flex items-center flex-shrink-0 text-white mr-6">
<svg
class="fill-current h-8 w-8 mr-2"
width="54"
height="54"
viewBox="0 0 54 54"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M13.5 22.1c1.8-7.2 6.3-10.8 13.5-10.8 10.8 0 12.15 8.1 17.55 9.45 3.6.9 6.75-.45 9.45-4.05-1.8 7.2-6.3 10.8-13.5 10.8-10.8 0-12.15-8.1-17.55-9.45-3.6-.9-6.75.45-9.45 4.05zM0 38.3c1.8-7.2 6.3-10.8 13.5-10.8 10.8 0 12.15 8.1 17.55 9.45 3.6.9 6.75-.45 9.45-4.05-1.8 7.2-6.3 10.8-13.5 10.8-10.8 0-12.15-8.1-17.55-9.45-3.6-.9-6.75.45-9.45 4.05z"
/>
</svg>
<span class="font-semibold text-xl tracking-tight">Tailwind CSS</span>
</div>
<div class="block lg:hidden">
<button
class="flex items-center px-3 py-2 border rounded text-teal-200 border-teal-400 hover:text-white hover:border-white"
>
<svg class="fill-current h-3 w-3" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
<title>Menu</title>
<path d="M0 3h20v2H0V3zm0 6h20v2H0V9zm0 6h20v2H0v-2z" />
</svg>
</button>
</div>
<div class="w-full block flex-grow lg:flex lg:items-center lg:w-auto">
<div class="text-sm lg:flex-grow">
<a
href="#responsive-header"
class="block mt-4 lg:inline-block lg:mt-0 text-teal-200 hover:text-white mr-4"
>Docs</a>
<a
href="#responsive-header"
class="block mt-4 lg:inline-block lg:mt-0 text-teal-200 hover:text-white mr-4"
>Examples</a>
<a
href="#responsive-header"
class="block mt-4 lg:inline-block lg:mt-0 text-teal-200 hover:text-white"
>Blog</a>
</div>
<div>
<a
href="#"
class="inline-block text-sm px-4 py-2 leading-none border rounded text-white border-white hover:border-transparent hover:text-teal-500 hover:bg-white mt-4 lg:mt-0"
>Download</a>
</div>
</div>
</nav>
</template>

文章.vue

<template>
<div>
<h1>Articles</h1>
</div>
</template>

<script>
export default {
data() {
return {
articles: [],
article: {
id: "",
title: "",
body: ""
},
article_id: "",
pagination: {},
edit: false
};
},

created() {
this.fetchArticles();
},

methods: {
fetchArticles() {
fetch("api/articles")
.then(res => res.json())
.then(res => {
console.log(res.data);
});
}
}
};
</script>

最佳答案

这是一个非常小的错误(bug?),很容易被忽视。除了一件事之外,你一切都做对了。由于以下原因,代码未按预期工作:<navigation /> 。如果您将其更改为:<navigation></navigation>它将按预期工作。

我不确定这个问题的确切原因,但我在某处读到过有关 Vue 不支持自终止标签的内容,因为它们在 HTML5 中无效,但我不确定。也许有人可以详细说明这一点。

关于javascript - 在 laravel 6.x 中渲染多个 vue 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60344481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com