How can I include Fontawesome 6 in Nuxt 3 project? - vuejs3

first of all my configuration:
nuxt.config.ts
import { defineNuxtConfig } from 'nuxt'
export default defineNuxtConfig({
// if you want to use static HTML generation (SSG)
target: 'static',
// if you want to use server-side rendering (SSR)
ssr: true,
css: [
'assets/scss/main.css',
'assets/scss/header.css',
'#fortawesome/fontawesome-svg-core/styles.css'
],
build: {
transpile: [
'#fortawesome/vue-fontawesome',
'#fortawesome/fontawesome-svg-core',
'#fortawesome/pro-solid-svg-icons',
'#fortawesome/pro-regular-svg-icons',
'#fortawesome/free-brands-svg-icons'
]
}
})
/plugins/fontawesome.js
import { defineNuxtPlugin } from '#app';
/** Fontawesome for Nuxt 3
* https://fontawesome.com/docs/web/use-with/vue/use-with#nuxt
*
*/
// import the fontawesome core
import { library, config } from '#fortawesome/fontawesome-svg-core'
// import font awesome icon component
import { FontAwesomeIcon } from '#fortawesome/vue-fontawesome'
// import specific icons
import { fas } from '#fortawesome/free-solid-svg-icons'
// This is important, we are going to let Nuxt worry about the CSS
config.autoAddCss = false
export default defineNuxtPlugin((nuxtApp) => {
console.log('[Plugin]', 'Font Awesome')
// You can add your icons directly in this plugin. See other examples for how you
// can add other styles or just individual icons.
library.add(fas);
nuxtApp.vueApp.component('font-awesome-icon', FontAwesomeIcon)
})
<template>
<header class="header">
<Logo />
coffee <font-awesome-icon icon="fas coffee" />
flag <font-awesome-icon icon="fas fa-flag" />
search <font-awesome-icon icon="fas fa-magnifying-glass" />
search 2 <font-awesome-icon icon="fa-solid fa-magnifying-glass" />
<nav>
<nuxt-link :to="{ path: '/' }">
Home
</nuxt-link>
<nuxt-link :to="{ path: '/getting-started' }">
Getting Started
</nuxt-link>
<nuxt-link :to="{ path: '/faq' }">
FAQ
</nuxt-link>
<nuxt-link :to="{ path: '/search' }">
<font-awesome-icon icon="fas fa-magnifying-glass" />
</nuxt-link>
</nav>
</header>
</template>
Problem
When I start npx nuxi dev and look at the page via localhost, the icons appear for 1 second and then are no longer visible. I have been searching for a solution for quite some time because I could not find an error right away.
Warning - console
[Vue warn]: Hydration node mismatch:
- Client vnode: font-awesome-icon
- Server rendered DOM: <svg class=​"svg-inline--fa fa-magnifying-glass" style aria-hidden=​"true" focusable=​"false" data-prefix=​"fas" data-icon=​"magnifying-glass" role=​"img" xmlns=​"http:​/​/​www.w3.org/​2000/​svg" viewBox=​"0 0 512 512">​…​</svg>​
at <Navigation>
at <Default >
at <AsyncComponentWrapper >
at <BaseTransition mode="out-in" appear=false persisted=false ... >
at <Transition name="layout" mode="out-in" >
at <Anonymous>
at <App key=1 >
at <NuxtRoot>

You can wrap the component in a <client-only>, so it only renders client-side.
<client-only>
<font-awesome-icon icon="fas fa-flag" />
</client-only>

Related

TypeError: can't access property "nodeValue", node is null

In vue3 component I have this code:
<script>
export default {
data() {
return {
vendedores: [{
id: 1,
nombre: "Name test1"
}],
};
}
};
</script>
Having this template, there everything works fine:
<div v-for="vendedor in vendedores"
:key="vendedor.id">
</div>
But in this one is displaying an error everytime vite compiles the assets:
<div v-for="vendedor in vendedores"
:key="vendedor.id">
<button
:class="(vendedor.seleccionado)? 'bg-zinc-800 border-amber-300' : 'bg-transparent'"
class="border-zinc-400 rounded-full py-3 text-left hover:border-violet-200"
>
<span> Test</span>
</button>
</div>
The error displayed in the console is the following:
TypeError: can't access property "nodeValue", node is null
setText runtime-dom.esm-bundler.js:30
processText runtime-core.esm-bundler.js:5109
patch runtime-core.esm-bundler.js:5064
patchKeyedChildren runtime-core.esm-bundler.js:5849
patchChildren runtime-core.esm-bundler.js:5792
patchElement runtime-core.esm-bundler.js:5305
processElement runtime-core.esm-bundler.js:5165
patch runtime-core.esm-bundler.js:5082
...
I have updated #vite to last version (^3.2.0) and vue3 (3.2.41). Other components working fine in other projects with the same version.

Failed to compile ./styles/globals.css:4:0 Module not found: Can't resolve './${imagesat}image-hero-mobile.png'

I am getting this error in
./styles/globals.css:4:0
Module not found: Can't resolve './${imagesat}image-hero-mobile.png'
Import trace for requested module:
./styles/globals.css
./pages/_app.tsx
I am using next js as framework this error is probally due to tailwindcss .
'./${imagesat}image-hero-mobile.png' this line of code is only in one file that is
import Head from "next/head";
import Image from "next/image";
import menuList, { menuListType, menuObjectType } from "./menu";
const imagesat = "/images/01introSectionPage/";
const clients = [
"client-audiophile.svg",
"client-databiz.svg",
"client-maker.svg",
"client-meet.svg",
];
const IntroSectionDropdown = () => {
return (
<> {/* <div className="right">
<div
className={`bg-[url('${imagesat}image-hero-mobile.png')] md:bg-[url('${imagesat}image-hero-desktop.png')] h-32 w-32 bg-no-repeat bg-cover`}
>
<span className="sr-only">Hero image</span>
</div>
<img
src={`${imagesat}image-hero-desktop.png`}
alt=""
className="hidden md:block"
/>
<img
src={`${imagesat}image-hero-mobile.png`}
alt=""
className="md:hidden"
/>
</div> */}
</>
);
};
export default IntroSectionDropdown;
If i commmet the #tailwind utilities; in globals.css then website goes on but without it tailwind is not complete.
Where i used the './${imagesat}image-hero-mobile.png' is commented the also getting error.
i had tried removing tailwind and reinstalled
i had cleard the next cache
I hope anyone has solution
REPO - https://github.com/Chandraprakash-Darji/frontend-mentor-challenges

vuejs vue-router isn't working with Firebase hosting

I'm having a issue while using vue router. When I click on the Navbar to change between views/pages, the website works fine, but when I try to go directly to a specific page through the domain/url, I get an error through Firebase (where the website is hosted) saying that the file does not exist in the root index.html.
Here's the code of the index.js file from routes:
import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import Courses from "../views/Courses.vue";
import LandingPageTechSession from "../views/LandingPageTechSession.vue";
import VueMeta from "vue-meta";
Vue.use(VueMeta);
Vue.use(VueRouter);
const routes = [
{
path: "/",
name: "Home",
component: Home,
},
{
path: "/muddy-match",
name: "MuddyMatch",
component: MuddyMatch,
},
{
path: "/courses",
name: "Courses",
component: Courses,
},
{
path: "/techsession",
name: "TechSession",
component: LandingPageTechSession,
},
];
const router = new VueRouter({
mode: "history",
base: process.env.BASE_URL,
routes,
});
export default router;
Here's the code of the App.vue:
<div id="app">
<link
rel="stylesheet"
href="https://use.fontawesome.com/releases/v5.2.0/css/all.css"
integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ"
crossorigin="anonymous"
/>
<navbar></navbar>
<router-view />
<bottom></bottom>
</div>
</template>
<script>
import Navbar from "./components/Navbar.vue";
import Bottom from "./components/Footer.vue";
export default {
name: "App",
components: {
Navbar,
Bottom,
},
};
And the Navbar item. (The courses route is with router-link but it still gives the same error):
<template>
<b-container fluid>
<b-navbar toggleable="sm" id="navbar" type="light">
<b-navbar-brand :to="{ name: 'Home' }">
<img id="logo" src="../assets/S2Plogo.png" alt="S2Plogo" />
</b-navbar-brand>
<b-navbar-toggle target="nav-collapse"></b-navbar-toggle>
<b-collapse id="nav-collapse" class="collapse" is-nav>
<b-navbar-nav align="end" class=" ml-auto">
<b-nav-item size="sm" class="navbar-item"
><router-link class="navbar-item" :to="{ path: 'Courses' }">{{
$t("nav.courses")
}}</router-link>
</b-nav-item>
<b-nav-item
size="sm"
class="navbar-item"
:to="{ name: 'TechSession' }"
>{{ $t("nav.techsessions") }}</b-nav-item
>
<b-nav-item
size="sm"
class="navbar-item"
href="https://teespring.com/"
target="_blank"
>{{ $t("nav.merchandise") }}</b-nav-item
>
<b-nav-item size="sm" class="navbar-item" href="/#contact-us">{{
$t("nav.contactus")
}}</b-nav-item>
<b-nav-item
size="sm"
v-on:click="switchLocale()"
class="navbar-item"
>{{ displayLocale }}</b-nav-item
>
</b-navbar-nav>
</b-collapse>
</b-navbar>
</b-container>
</template>
Thank you advance guys!
The answer about this has nothing to do with Vue Router or Vue actually, but with Firebase.
If you want to be able to navigate through url directly, just add the "rewrites" array on your firebase.json:
{
"hosting": {
"public": "dist",
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
},
}
You shouldn't have any issue now.
Happy coding!
It seems like the issue you're facing is connected to the vue-router history mode.
mode: "history",
When using createWebHistory(), the URL will look "normal," e.g. https://example.com/user/id. Beautiful!
Here comes a problem, though: Since our app is a single page client side app, without a proper server configuration, the users will get a 404 error if they access https://example.com/user/id directly in their browser. Now that's ugly.
Not to worry: To fix the issue, all you need to do is add a simple catch-all fallback route to your server. If the URL doesn't match any static assets, it should serve the same index.html page that your app lives in. Beautiful, again!
Please find more details in the documentation of vue-router: vue-router history mode
Hope this helps!

Image not showing up in react js using require.context

I tried to search for the answer but I am not getting anything to solve it.
I am loading my image using require.context as you can see in the code but it's not getting loaded. It used to work perfectly before in previous versions of react js. Now I am using react version 17.0.1. There are no errors in the console. If I import the image and use it in the src it works fine. I have also tried to change the images with some previous images used in previous projects (using react version 16.x.x) which are working fine there. I am creating react app using npx-create-react-app. Path to image is correct as in case of incorrect path "module named xxx not found error occurs".
Current behavior:
Image not showing up instead alt value is showing up.
Desired behavior:
Image should show up instead of alt value.
import React, { Component } from "react";
import commonStyles from "../css/common.module.css";
import loginStyles from "../css/login.module.css";
import { TextField, Button, Paper, Typography } from "#material-ui/core";
class Login extends Component {
state = {
userName: "",
password: "",
error: "",
};
render() {
const images = require.context("../images", true);
return (
<div
className={`${loginStyles.root} d-flex justify-content-center align-items-center ${commonStyles.bg}`}
>
<Paper
classes={{
root: `${commonStyles.paper} mt-2`,
}}
elevation={3}
>
<div className={`${loginStyles.child}`}>
<div className={`d-flex justify-content-center align-items-center`}>
<img
src={images(`./Shahmeer.png`)}
alt={`Shahmeer Avenue Logo`}
width="100"
height="100"
/>
</div>
<Typography
classes={{
root: `font-weight-bold`,
}}
variant="h5"
gutterBottom
>
Login
</Typography>
<form noValidate autoComplete="off">
<TextField
classes={{
root: `${commonStyles.textField}`,
}}
onChange={(e) => this.handleChange(e)}
id={"userName"}
label={"User Name"}
variant="outlined"
error={this.state.error ? true : false}
helperText={this.state.error}
value={this.state["userName"]}
/>
<TextField
classes={{
root: `${commonStyles.textField}`,
}}
onChange={(e) => this.handleChange(e)}
id={"password"}
label={"Password"}
variant="outlined"
error={this.state.error ? true : false}
helperText={this.state.error}
value={this.state["password"]}
/>
<div className={`w-100 d-flex justify-content-end mt-2`}>
<Button variant="contained" color="primary">
Login
</Button>
</div>
</form>
</div>
</Paper>
</div>
);
}
}
export default Login;
snapshot of browser:
You should use the default property for the images:
<img
src={images(`./Shahmeer.png`).default}
alt={`Shahmeer Avenue Logo`}
width="100"
height="100"
/>

Why do the Bootstrap 4 elements reload entire Angular 7 application?

I am working with bootstrap 4 and angular 7, I have the following element, which function is to hide/show a sidebar.
<a
class="app-sidebar__toggle"
href="#"
data-toggle="sidebar"
aria-label="Hide Sidebar"
></a>
The problem is when I enter to specific route, this reloads all page. These are my routes in app-routing.module.ts
const routes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'catalogo/lista', component: CatalogoListaComponent },
{ path: '', redirectTo: 'home', pathMatch: 'full' }
];
So, if I enter to http://localhost:4200/home, the error will happen, but if I enter to any other route (my default empty route will redirect to /home), for example, http://localhost:4200 or http://localhost:4200/a_route_that_not_exists, I am redirected to /home (as expected), and the button that show/hide the sidebar works well.
I hope to be clear in my problem and I would like a lot you can help me.
EDIT: More code of my application...
This is my app.component.html:
<app-header></app-header>
<app-sidebar></app-sidebar>
<div class="app-content">
<app-title [titulo]="titulo" [icono]="icono [breadcrumbs]="breadcrumbs"></app-title>
<router-outlet></router-outlet>
</div>
This is my header.component.html (where I use the link to show/hide the sidebar):
<header class="app-header">
<a class="app-header__logo" href="index.html">Vali</a>
<!-- Sidebar toggle button-->
<a
class="app-sidebar__toggle"
href="#"
data-toggle="sidebar"
aria-label="Hide Sidebar"
></a>
<p>.... more html</p>
</header>
And this is my sidebar.component.html:
<div class="app-sidebar__overlay" data-toggle="sidebar"></div>
<aside class="app-sidebar">
<div class="app-sidebar__user">
<img
class="app-sidebar__user-avatar"
src="https://s3.amazonaws.com/uifaces/faces/twitter/jsa/48.jpg"
alt="User Image"
/>
<div>
<p class="app-sidebar__user-name">John Doe</p>
<p class="app-sidebar__user-designation">Frontend Developer</p>
</div>
</div>
<ul class="app-menu">
<li>
<a class="app-menu__item" [routerLink]="['/home']">
<i class="app-menu__icon fa fa-home"></i>
<span class="app-menu__label">Inicio</span>
</a>
</li>
more menu elements...
<ul>
</aside>
Generally a lot of the Popper/ JS elements in Bootstrap won't work out of the box in Angular; Angular provides a pretty solid way to handle elements like sidenavs.
Since the element you want to use to toggle the sidenav doesn't exist in the same component as the sidenav, you can set up a basic service that handles the sidenav state. To create your sidenav service (run in console in your project root):
ng g s sidenav
And then in the generated sidenav.service.ts:
import {Injectable} from '#angular/core';
import {BehaviorSubject} from 'rxjs';
#Injectable()
export class SidenavService {
public isOpen: boolean = false;
public toggleChange: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
constructor() {}
public toggle(): void {
this.isOpen = !this.isOpen;
this.toggleChange.next(this.isOpen);
}
}
In your header.component.html you can modify the clickable element that will show/ hide the sidebar:
<a
class="app-sidebar__toggle"
aria-label="Hide Sidebar"
(click)="toggleSidebar()"
></a>
In your header.component.ts file you can then set toggleSidebar() to run the toggle() function in the service you just set up:
import {SidenavService} from "[wherever your service is located]";
#Component({ /*...*/ })
export class HeaderComponent {
constructor(private sidenavService: SidenavService)
toggleSidebar(): void {
this.sidenavService.toggle();
}
}
You can then (in either your app component or sidebar component) set up reacting to the toggle:
//assuming you're in sidebar.component.ts
import {SidenavService} from "[wherever your service is located]";
import {OnInit, OnDestroy} from "#angular/core";
import {Subscription} from "rxjs";
#Component({ /*...*/ })
export class SidebarComponent implement OnInit, OnDestroy {
isOpen: boolean;
sidenavSubscription: Subscription;
constructor(private sidenavService: SidenavService) {}
ngOnInit() {
this.sidenavSubscription = this.sidenavService.toggleChange.subscribe(isOpenChange => {
this.isOpen = isOpenChange;
});
}
ngOnDestroy() {
this.sidenavSubscription.unsubscribe();
}
}
You can then use the isOpen variable in your sidebar component in different ways to control sidebar activity. Examples include using an [ngClass] directive:
<!--in your header component-->
<header [ngClass]={'active': isOpen, 'inactive': !isOpen} >
</header>
Or you can build in something using angular animations to animate the sidebar in and out (using ngIf and the :enter/ :leave transitions).

Resources