Vue.js how to import SVG logo - reactjs

I'm new to vue.js and am used to React. I'm currently trying to import an SVG logo into my header component but I'm not sure how. In react, I would simply do import Logo from './path; and use Logo wherever I needed it within the current component. This is basically what I'm attempting to do right now but I keep getting errors. Could anyone tell me how this could be done in Vue.js?
<template>
<header class="nav">
<img src={Logo} alt="24G Logo">
</header>
</template>
<script>
import Logo from '../assets/76_logo.svg';
export default {
name: 'Header'
}
</script>
<style lang="scss" scoped>
</style>

Here are three options. The best in my opinion is the third:
Simply input src like in any webpage <img src='../path/to/file.svg' ... though that come with some drawbacks (regardless if it's :src='logoPath' where logoPath is variable containing the same. For a short overview see this stack answer, and for more details see this article from css tricks.
Check out svg-vue-loader. Vue won't automatically import svg without a loader.
Just paste it in! (Open the svg file and copy paste it into the template.) The best option in my opinion, especially when prototyping or for smaller projects. Like so:
<template>
<header class="nav">
<svg ....
</header>
</template>
If that would make it too crowded later on, just make a new component, call it say Logo, and paste svg in there and then import MainLogo component into your header.
No need for svg-loaders. Though loaders are a dev dependency, so not like it would cost you anyway; they would just do the same thing you can do manually.
// in MainLogo.vue
<template>
<svg ....
</template>
// in MainHeader.vue
<template>
<header class="nav">
<MainLogo>
</header>
</template>
<script>
import MainLogo from '../path/to/file.vue'
export default {
components: { MainLogo }
}
</script>
Cheers

After searching and searching, and seeing all the answers were old, I went ahead and tried the newish v-html prop.
The result, success!
<div v-html="avatar" style="width: 100%"></div>
The avatar is a full element that I stored in the database.
No loaders, no imports, just using the built in resources of Vue.js
If you leave out the style, then the svg will not show.
Also, loading the full element enables me to attach a ref prop to the element. Enabling me to access the svg through script.
Hope that helps someone!!
I used this with avataaar's random avatar generator and stored the resulting svg to the database (mongo)

Here is another approach that I used:
<template>
<a href="#"
class="log-link-css-class">
<!-- SVG Icon Start-->
<img alt="alt message" class="your-logo-css-class"
src="#/assets/images/logofilename.svg">
<!-- <SVG Icon End /> -->
</a>
</template>
No import required. Vue automatically converts it to the unique URL.

edit your code
<script>
import Logo from '../assets/76_logo.svg';
export default {
name: 'Header',
data(){
Logo: Logo
}
}

Related

Wrong breadcrumb rendered in sveltekit

What I want
is to have a Breadcrumb component rendered in the __layout.svelte and using a writable store $breadcrumb to read the data to display. The $breadcrumb data is filled in every route.svelte file with data provided by a loader
The problem
When the page is served (yarn dev) or rendered (yarn build with adapter-static) the breadcrumb has the data from another route and then gets updated when the page is rehydrated. Basically, it looks like the Breadcrumb.svelte component is rendered before the data is loaded in the view hence using the old store value from the previous page.
Rendering the Breadcrumb component from every single route fixes the issue but I would like to keep it in the __layout file.
Any idea on what to do ?
The code
__layout.svelte
<script>
import Breadcrumb from '../components/Breadcrumb.svelte'
</script>
<div class="wrap">
<MainMenu />
<div class="container">
<Breadcrumb />
<slot />
</div>
...
store.js
import { writable } from 'svelte/store';
export const breadcrumb = writable({});
components/Breadcrumb.svelte
<script>
import { breadcrumb } from '$lib/store';
</script>
{#if Object.keys($breadcrumb).length}
<ul class="breadcrumb noprn">
<li>Accueil</li>
{#each Object.entries($breadcrumb) as [label, url] (label)}
<li>
{#if url}
<a href={url}>{label}</a>
{:else}
<h1>{label}</h1>
{/if}
</li>
{/each}
</ul>
{/if}
routes/product/[id].svelte
<script>
export let reference; // << the data comes in here
let product = reference.product;
import { breadcrumb } from '$lib/store';
$breadcrumb = {
Catalogue: '/catalogue',
[reference.category.name]: `/catalogue/${reference.category.slug}`,
[reference.brand.name]: `/catalogue/brands/${reference.brand.id}`,
[product.name]: null
}
</script>
...

Issue with lightning carousel

I am facing some issues with a lightning carousel that I am building in lwc like it is showing only 5 to 6 images inside the carousel and remaining are not showing up. If there is any limitation for the number of images could you please suggest the best approach or alternatives like HTML slideshow or any other way to build a carousel for 1000's of images inside the carousel.
Here I am loading the images from the parent component.
import { LightningElement, api } from "lwc";
export default class fcxmCarousel extends LightningElement {
#api selectdImage;
#api value;
}
<template>
<div class="slds-m-around_medium">
<div class="container">
<lightning-carousel>
<template for:each={value} for:item="rows">
<lightning-carousel-image width="100%" height="50%" key={imageURL} src = {rows.imageURL}
header={rows.imageName}
description={rows.imageDescription}>
</lightning-carousel-image>
</template>
</lightning-carousel>
</div>
</div>
<!-- </lightning-card> -->
</template>
Yes, we can only show 5-6 images using standard lightning-carousel component, for more details reefer this link https://developer.salesforce.com/docs/component-library/bundle/lightning:carousel

How to include the same React app twice on the same page?

I have a simple app created using CRA v2 that provides a "load more" button after lists of posts. The default posts displayed on the page are generated server-side based on a set of criteria (ie. specific post type, taxonomy terms, etc), and the "load more" button queries an API to display more posts that match the same criteria.
My pages will have an undefined (but >1) number of post lists on a page, and not all of the lists will be nearby each other, so the whole thing can't exist in a single app. I need to be able to render the app more than once per-page and have them operate independently.
Best case scenario, I'd be able to do something like this:
<ul class="posts posts--foo">[first list of posts from the "foo" post type go here]</ul>
<div id="app-root" data-post-type="foo"></div>
<ul class="posts posts--bar">[second list of posts from the "bar" post type go here]</ul>
<div id="app-root" data-post-type="bar"></div>
<script src="main.7a3cc682.js"></script> <!-- built script-->
I realize this won't work as written. Is this possible, and if so what's the best way to make this work?
I was able to find a solution using the answer to this question. Here's what it looks like:
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import render from 'react-dom';
import App from './App';
window.mount = function(id) {
let app = document.getElementById(id);
ReactDOM.render( <WPLoadMore {...(app.dataset)} />, document.getElementById(id) );
}
and then in my HTML:
<script src="build/static/js/main.7a3cc682.js"></script>
<ul class="posts posts--foo"></ul>
<div id="app1" data-post-type="foo"></div>
<script type="text/javascript">mount("app1");</script>
<ul class="posts posts--bar"></ul>
<div id="app2" data-post-type="bar"></div>
<script type="text/javascript">mount("app2");</script>
The only slightly wonky bit about this is that in the index.html in my public directory, I needed to move the mount() outside of the </body> tag so that it loads after all of the React scripts, like so:
</body>
<script type="text/javascript">mount("wplm");</script> <!-- included outside the body so this works in development -->
</html>

Font awesome icons are not displaying react

I'm using font awesome and have imported it in index.js
import '././styles/fontawesome/css/fontawesome.min.css';
following in filter component
<span onClick={this.togglem.bind(this)}>
{/*<span className="rTitle">Filters</span>*/}
<i className="fa fa-filter" aria-hidden="true"></i>
</span>
Here, the icon fa-filter is not getting displayed can anyone lemme know whats going wrong
Using double dots its throws this exception:
You attempted to import
../../../styles/fontawesome/css/fontawesome.min.css which falls outside of the project src/ directory. Relative imports outside of
src/ are not supported.
structure is
src/filter.js
src/styles/fontawesome
version -5.5.0
i've imported in filter pointings are all right it show the css styles
when inspected but does not show the icon
Your import looks wrong import '././styles/fontawesome/css/fontawesome.min.css'; you're probably looking for import '../../styles/fontawesome/css/fontawesome.min.css'; (pay attention to the dots).
If you're using React with JSX (you're most likely doing it) you need to use className to define html class attribute:
<i className="fa fa-filter" aria-hidden="true"></i>
Also, the right way to use FontAwesome in React in documented here: https://fontawesome.com/how-to-use/on-the-web/using-with/react

ReactBootstrap global is undefined

so I'm currently working on a project written in Flask which I added Bootstrap to. I have recently started adding some react components and I would like to have them look like the other bootstrapped components. Here is some of my code:
This is where I've included my bootstrap.js files and react.js file
<script src="/static/js/bootstrap.min.js"></script>
<script src="/static/js/build/react.js"></script>
<script src="/static/js/build/JSXTransformer.js"></script>
<script src="/static/js/react-bootstrap-bower/react-bootstrap.js"></script>
This html normally is rendered by bootstrap as wonderful little pager icons, but not in react:
return (
<div>
<nav>
<ul className="pagination pagination-lg">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
I understand I didn't put the full return, the return isn't the issue at hand, however, its the styling. I tried adding in a Pager element with the following
var Pager = ReactBootstrap.Pager;
...
//inside some html return
<Pager></Pager>
but I get the response first that ReactBootstrap is not defined. Yet I read that the ReactBootstrap global is created automatically when you include the js file. The other response is that Pager doesn't exist (obviously since ReactBootstrap was unable to return it).
So my question is, can I simply return html without the data-reactid info or somehow have it rendered by bootstrap without needing reactBootstrap? or do is there something I'm missing from my files to use reactBootstrap. Thanks in advance for the help!
I tried to reproduce it in a jsbin, but ReactBootstrap.Pager is a component, and it renders with the correct styles.
You're not loading things correctly.

Resources