vue组件库的正确使用方法是什么?
What is the correct way to use vue component libraries?
我正在尝试在 Laravel 项目中使用一些外部 vue 组件库,但我最初尝试使用 vue-avatar、vue notification bell 和 pwa-install 都没有成功。
对于前两个,一切似乎都已检查完毕,没有构建错误,组件实际上显示在 html 中,但没有头像或通知铃。没有迹象表明外部包已导入到我的组件中。
这些是我采取的步骤:
- 根据需要 npm 安装 vue-avatar 和 notification-bell。
- 我为每个组件创建了一个组件,导入了每个已安装的包并进行了默认导出。
- 然后我像往常一样在另一个文件中使用我的组件。
这些代码基本上是样板文件,没有什么可添加的,所以我不知道为什么我没有得到预期的输出。
如有任何帮助,我们将不胜感激。
这是我的头像组件代码:
<template>
<div></div>
</template>
<script>
import Avatar from 'vue-avatar'
export default {
components: {
Avatar
}
}
</script>
这是我使用它的布局文件的代码:
<template>
<div>
<div class="min-h-screen bg-gray-100">
<nav class="bg-white border-b border-gray-100">
<!-- Primary Navigation Menu -->
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between h-16">
<div class="flex">
<!-- Logo -->
<div class="flex-shrink-0 flex items-center">
<inertia-link :href="route('dashboard')">
<breeze-application-logo class="block h-9 w-auto" />
</inertia-link>
</div>
<!-- Navigation Links -->
<div class="hidden space-x-8 sm:-my-px sm:ml-10 sm:flex">
<breeze-nav-link :href="route('dashboard')" :active="route().current('dashboard')">
Dashboard
</breeze-nav-link>
</div>
</div>
<div v-if="$page.props.flash.message" class="alert">
{{ $page.props.flash.message }}
</div>
<!-- <div>
<broadcast-message></broadcast-message>
</div> -->
<avatar :username="$page.props.auth.user.first_name"></avatar>
<notification-bell />
<div class="hidden sm:flex sm:items-center sm:ml-6">
<!-- Settings Dropdown -->
<div class="ml-3 relative">
<breeze-dropdown align="right" width="48">
<template #trigger>
<span class="inline-flex rounded-md">
<button type="button" class="inline-flex items-center px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md text-gray-500 bg-white hover:text-gray-700 focus:outline-none transition ease-in-out duration-150">
{{ $page.props.auth.user.first_name }}
<svg class="ml-2 -mr-0.5 h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd" />
</svg>
</button>
</span>
</template>
<template #content>
<breeze-dropdown-link :href="route('logout')" method="post" as="button">
Log Out
</breeze-dropdown-link>
</template>
</breeze-dropdown>
</div>
</div>
<!-- Hamburger -->
<div class="-mr-2 flex items-center sm:hidden">
<button @click="showingNavigationDropdown = ! showingNavigationDropdown" class="inline-flex items-center justify-center p-2 rounded-md text-gray-400 hover:text-gray-500 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 focus:text-gray-500 transition duration-150 ease-in-out">
<svg class="h-6 w-6" stroke="currentColor" fill="none" viewBox="0 0 24 24">
<path :class="{'hidden': showingNavigationDropdown, 'inline-flex': ! showingNavigationDropdown }" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" />
<path :class="{'hidden': ! showingNavigationDropdown, 'inline-flex': showingNavigationDropdown }" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
</div>
</div>
<!-- Responsive Navigation Menu -->
<div :class="{'block': showingNavigationDropdown, 'hidden': ! showingNavigationDropdown}" class="sm:hidden">
<div class="pt-2 pb-3 space-y-1">
<breeze-responsive-nav-link :href="route('dashboard')" :active="route().current('dashboard')">
Dashboard
</breeze-responsive-nav-link>
</div>
<!-- Responsive Settings Options -->
<div class="pt-4 pb-1 border-t border-gray-200">
<div class="flex items-center px-4">
<div class="font-medium text-base text-gray-800">{{ $page.props.auth.user.first_name }}</div>
</div>
<div class="mt-3 space-y-1">
<breeze-responsive-nav-link :href="route('logout')" method="post" as="button">
Log Out
</breeze-responsive-nav-link>
</div>
</div>
</div>
</nav>
<!-- Page Heading -->
<header class="bg-white shadow" v-if="$slots.header">
<div class="max-w-7xl mx-auto py-6 px-4 sm:px-6 lg:px-8">
<slot name="header" />
</div>
</header>
<!-- Page Content -->
<main>
<slot />
</main>
</div>
</div>
</template>
<script>
import BreezeApplicationLogo from '@/Components/ApplicationLogo'
import BreezeDropdown from '@/Components/Dropdown'
import BreezeDropdownLink from '@/Components/DropdownLink'
import BreezeNavLink from '@/Components/NavLink'
import BreezeResponsiveNavLink from '@/Components/ResponsiveNavLink'
//import BroadcastMessage from '@/Components/BroadcastMessage'
import Avatar from '@/Components/DashboardAvatar'
import NotificationBell from '@/Components/DashboardNotificationBell'
export default {
props: {
username: String,
},
components: {
BreezeApplicationLogo,
BreezeDropdown,
BreezeDropdownLink,
BreezeNavLink,
BreezeResponsiveNavLink,
//BroadcastMessage,
Avatar,
NotificationBell,
},
data() {
return {
showingNavigationDropdown: false,
}
},
}
</script>
根据您的输入,我尝试按照建议在我的组件中使用导入,但我在控制台中收到 'maximum call stack exceeded' 错误。查看我的新代码:
<template>
<avatar :username="username"/>
</template>
<script>
import Avatar from 'vue-avatar'
export default {
props: {
username: String
},
name: 'DashboardAvatar',
components: {
Avatar
}
}
</script>
我直接导入使用的时候出现类型错误'cannot read 属性 _c of undefined.
我更正了我的组件中的一个错误:<avatar :username="username" />
不是 <dashboard-avatar>
并且该错误现在是与直接使用我上面提到的组件相同的类型错误。
我认为您可能误解了 Vue SFC 的工作原理。
您正在导入头像组件,但实际上并未使用它。
例如,正确的方法是:
<template>
<avatar v-bind="$attrs" v-on="$listeners"/>
</template>
<script>
import Avatar from 'vue-avatar'
export default {
name:'Avatar',
components: {
Avatar
}
}
</script>
这样你从vue-avatar导入组件,并在Avatar SFC中实际使用它,($attrs和$listeners只是将所有属性和事件绑定到组件)。完成此操作后,您的导入应该会起作用。
或者您可以直接在父组件中导入它,而无需先创建子组件(因为您没有自定义 vue-avatar 功能,而只是使用开箱即用的功能):
<script>
import BreezeApplicationLogo from '@/Components/ApplicationLogo'
import BreezeDropdown from '@/Components/Dropdown'
import BreezeDropdownLink from '@/Components/DropdownLink'
import BreezeNavLink from '@/Components/NavLink'
import BreezeResponsiveNavLink from '@/Components/ResponsiveNavLink'
//import BroadcastMessage from '@/Components/BroadcastMessage'
import Avatar from 'vue-avatar' <----------------------Use the module here directly.
import NotificationBell from '@/Components/DashboardNotificationBell'
export default {
props: {
username: String,
},
components: {
BreezeApplicationLogo,
BreezeDropdown,
BreezeDropdownLink,
BreezeNavLink,
BreezeResponsiveNavLink,
//BroadcastMessage,
Avatar,
NotificationBell,
},
...
您可以直接使用 vue 组件,至少在您达到其限制并添加一些自定义项或您喜欢的任何内容之前。您不必为该组件创建组件。
该组件的模板只是一个空 div。
如果你想为组件创建一个组件,你应该使用它,而不仅仅是导入它。我对 Vue 有点生疏,但我相信你可以为模板做这个:
<template>
<Avatar someAttribute="value" />
</template>
不过你也可以导入头像组件,直接在需要的地方使用<Avatar>
。
如果您想拥有一些您不想重复的应用范围内的属性,那么像您一样创建一个组件对于传递默认参数很有用。在这种情况下,您还应该将 props 转发给该组件,以便其他人可以自定义它。
我正在尝试在 Laravel 项目中使用一些外部 vue 组件库,但我最初尝试使用 vue-avatar、vue notification bell 和 pwa-install 都没有成功。
对于前两个,一切似乎都已检查完毕,没有构建错误,组件实际上显示在 html 中,但没有头像或通知铃。没有迹象表明外部包已导入到我的组件中。
这些是我采取的步骤:
- 根据需要 npm 安装 vue-avatar 和 notification-bell。
- 我为每个组件创建了一个组件,导入了每个已安装的包并进行了默认导出。
- 然后我像往常一样在另一个文件中使用我的组件。
这些代码基本上是样板文件,没有什么可添加的,所以我不知道为什么我没有得到预期的输出。
如有任何帮助,我们将不胜感激。
这是我的头像组件代码:
<template>
<div></div>
</template>
<script>
import Avatar from 'vue-avatar'
export default {
components: {
Avatar
}
}
</script>
这是我使用它的布局文件的代码:
<template>
<div>
<div class="min-h-screen bg-gray-100">
<nav class="bg-white border-b border-gray-100">
<!-- Primary Navigation Menu -->
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between h-16">
<div class="flex">
<!-- Logo -->
<div class="flex-shrink-0 flex items-center">
<inertia-link :href="route('dashboard')">
<breeze-application-logo class="block h-9 w-auto" />
</inertia-link>
</div>
<!-- Navigation Links -->
<div class="hidden space-x-8 sm:-my-px sm:ml-10 sm:flex">
<breeze-nav-link :href="route('dashboard')" :active="route().current('dashboard')">
Dashboard
</breeze-nav-link>
</div>
</div>
<div v-if="$page.props.flash.message" class="alert">
{{ $page.props.flash.message }}
</div>
<!-- <div>
<broadcast-message></broadcast-message>
</div> -->
<avatar :username="$page.props.auth.user.first_name"></avatar>
<notification-bell />
<div class="hidden sm:flex sm:items-center sm:ml-6">
<!-- Settings Dropdown -->
<div class="ml-3 relative">
<breeze-dropdown align="right" width="48">
<template #trigger>
<span class="inline-flex rounded-md">
<button type="button" class="inline-flex items-center px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md text-gray-500 bg-white hover:text-gray-700 focus:outline-none transition ease-in-out duration-150">
{{ $page.props.auth.user.first_name }}
<svg class="ml-2 -mr-0.5 h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd" />
</svg>
</button>
</span>
</template>
<template #content>
<breeze-dropdown-link :href="route('logout')" method="post" as="button">
Log Out
</breeze-dropdown-link>
</template>
</breeze-dropdown>
</div>
</div>
<!-- Hamburger -->
<div class="-mr-2 flex items-center sm:hidden">
<button @click="showingNavigationDropdown = ! showingNavigationDropdown" class="inline-flex items-center justify-center p-2 rounded-md text-gray-400 hover:text-gray-500 hover:bg-gray-100 focus:outline-none focus:bg-gray-100 focus:text-gray-500 transition duration-150 ease-in-out">
<svg class="h-6 w-6" stroke="currentColor" fill="none" viewBox="0 0 24 24">
<path :class="{'hidden': showingNavigationDropdown, 'inline-flex': ! showingNavigationDropdown }" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" />
<path :class="{'hidden': ! showingNavigationDropdown, 'inline-flex': showingNavigationDropdown }" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
</div>
</div>
<!-- Responsive Navigation Menu -->
<div :class="{'block': showingNavigationDropdown, 'hidden': ! showingNavigationDropdown}" class="sm:hidden">
<div class="pt-2 pb-3 space-y-1">
<breeze-responsive-nav-link :href="route('dashboard')" :active="route().current('dashboard')">
Dashboard
</breeze-responsive-nav-link>
</div>
<!-- Responsive Settings Options -->
<div class="pt-4 pb-1 border-t border-gray-200">
<div class="flex items-center px-4">
<div class="font-medium text-base text-gray-800">{{ $page.props.auth.user.first_name }}</div>
</div>
<div class="mt-3 space-y-1">
<breeze-responsive-nav-link :href="route('logout')" method="post" as="button">
Log Out
</breeze-responsive-nav-link>
</div>
</div>
</div>
</nav>
<!-- Page Heading -->
<header class="bg-white shadow" v-if="$slots.header">
<div class="max-w-7xl mx-auto py-6 px-4 sm:px-6 lg:px-8">
<slot name="header" />
</div>
</header>
<!-- Page Content -->
<main>
<slot />
</main>
</div>
</div>
</template>
<script>
import BreezeApplicationLogo from '@/Components/ApplicationLogo'
import BreezeDropdown from '@/Components/Dropdown'
import BreezeDropdownLink from '@/Components/DropdownLink'
import BreezeNavLink from '@/Components/NavLink'
import BreezeResponsiveNavLink from '@/Components/ResponsiveNavLink'
//import BroadcastMessage from '@/Components/BroadcastMessage'
import Avatar from '@/Components/DashboardAvatar'
import NotificationBell from '@/Components/DashboardNotificationBell'
export default {
props: {
username: String,
},
components: {
BreezeApplicationLogo,
BreezeDropdown,
BreezeDropdownLink,
BreezeNavLink,
BreezeResponsiveNavLink,
//BroadcastMessage,
Avatar,
NotificationBell,
},
data() {
return {
showingNavigationDropdown: false,
}
},
}
</script>
根据您的输入,我尝试按照建议在我的组件中使用导入,但我在控制台中收到 'maximum call stack exceeded' 错误。查看我的新代码:
<template>
<avatar :username="username"/>
</template>
<script>
import Avatar from 'vue-avatar'
export default {
props: {
username: String
},
name: 'DashboardAvatar',
components: {
Avatar
}
}
</script>
我直接导入使用的时候出现类型错误'cannot read 属性 _c of undefined.
我更正了我的组件中的一个错误:<avatar :username="username" />
不是 <dashboard-avatar>
并且该错误现在是与直接使用我上面提到的组件相同的类型错误。
我认为您可能误解了 Vue SFC 的工作原理。
您正在导入头像组件,但实际上并未使用它。
例如,正确的方法是:
<template>
<avatar v-bind="$attrs" v-on="$listeners"/>
</template>
<script>
import Avatar from 'vue-avatar'
export default {
name:'Avatar',
components: {
Avatar
}
}
</script>
这样你从vue-avatar导入组件,并在Avatar SFC中实际使用它,($attrs和$listeners只是将所有属性和事件绑定到组件)。完成此操作后,您的导入应该会起作用。
或者您可以直接在父组件中导入它,而无需先创建子组件(因为您没有自定义 vue-avatar 功能,而只是使用开箱即用的功能):
<script>
import BreezeApplicationLogo from '@/Components/ApplicationLogo'
import BreezeDropdown from '@/Components/Dropdown'
import BreezeDropdownLink from '@/Components/DropdownLink'
import BreezeNavLink from '@/Components/NavLink'
import BreezeResponsiveNavLink from '@/Components/ResponsiveNavLink'
//import BroadcastMessage from '@/Components/BroadcastMessage'
import Avatar from 'vue-avatar' <----------------------Use the module here directly.
import NotificationBell from '@/Components/DashboardNotificationBell'
export default {
props: {
username: String,
},
components: {
BreezeApplicationLogo,
BreezeDropdown,
BreezeDropdownLink,
BreezeNavLink,
BreezeResponsiveNavLink,
//BroadcastMessage,
Avatar,
NotificationBell,
},
...
您可以直接使用 vue 组件,至少在您达到其限制并添加一些自定义项或您喜欢的任何内容之前。您不必为该组件创建组件。
该组件的模板只是一个空 div。
如果你想为组件创建一个组件,你应该使用它,而不仅仅是导入它。我对 Vue 有点生疏,但我相信你可以为模板做这个:
<template>
<Avatar someAttribute="value" />
</template>
不过你也可以导入头像组件,直接在需要的地方使用<Avatar>
。
如果您想拥有一些您不想重复的应用范围内的属性,那么像您一样创建一个组件对于传递默认参数很有用。在这种情况下,您还应该将 props 转发给该组件,以便其他人可以自定义它。