svelte

How can I fix localStorage error in sapper/svelte

我的梦境 提交于 2021-02-11 04:31:06
问题 import { writable } from 'svelte/store'; /** Read the current token from LocalStorage on boot */ const token = writable(localStorage.getItem('token')); /** Store the token in LocalStorage whenever it´s updated */ token.subscribe((val) => { if (!localStorage) return; if (!val) { return localStorage.removeItem('token'); } localStorage.setItem('token', val); }); export default token; I still get an error that localStorage is undefined in sapper when I run npm run dev 回答1: In Sapper, your code

Emit truly “raw” html in Svelte

前提是你 提交于 2021-02-10 06:53:32
问题 I'm receiving raw html snippets from a headless CMS that I need to render as-is in a Sapper application. This also includes cases where I receive an opening tag and the corresponding closing tag as two pieces of HTML between which I add a Svelte component. <script> import MyComponent from './MyComponent.svelte' // example snippets coming from headless CMS const prefix = '<p class="test">' const suffix = '</p>' </script> <!-- use snippets as-is --> {@html prefix} <MyComponent /> {@html suffix}

Emit truly “raw” html in Svelte

妖精的绣舞 提交于 2021-02-10 06:51:39
问题 I'm receiving raw html snippets from a headless CMS that I need to render as-is in a Sapper application. This also includes cases where I receive an opening tag and the corresponding closing tag as two pieces of HTML between which I add a Svelte component. <script> import MyComponent from './MyComponent.svelte' // example snippets coming from headless CMS const prefix = '<p class="test">' const suffix = '</p>' </script> <!-- use snippets as-is --> {@html prefix} <MyComponent /> {@html suffix}

How to expose a function in svelte that can accept parameters to render?

那年仲夏 提交于 2021-02-10 05:28:06
问题 I'm pretty new to svelte, and for my use case I would like to export the svelte app as a bundle.js which exposes a function let's say startApp(positionInject, appConfiguration) that can accept 2 parameters ( positionInject is the app injection position eg: .app, appConfiguration is the initial configuration of the app to start), based on those parameters svelte app start renders. I am wondering, this is possible in svelte? Appreciate any help. 回答1: Every Svelte component takes a target

How to expose a function in svelte that can accept parameters to render?

我的未来我决定 提交于 2021-02-10 05:25:56
问题 I'm pretty new to svelte, and for my use case I would like to export the svelte app as a bundle.js which exposes a function let's say startApp(positionInject, appConfiguration) that can accept 2 parameters ( positionInject is the app injection position eg: .app, appConfiguration is the initial configuration of the app to start), based on those parameters svelte app start renders. I am wondering, this is possible in svelte? Appreciate any help. 回答1: Every Svelte component takes a target

Function.prototype.toString issues in IE11 Svelte/Babel/Rollup

回眸只為那壹抹淺笑 提交于 2021-02-08 07:22:00
问题 My current rollup.config.js is commonjs(), babel({ extensions: ['.js', '.mjs', '.html', '.svelte'], runtimeHelpers: true, exclude: ['node_modules/@babel/**', 'node_modules/core-js/**' ], // <= /!\ NOT 'node_mobules/**' presets: [ ['@babel/preset-env', { // adapter to ensure IE 11 support targets: '> 0.25%, not dead, IE 11', "modules": false, "spec": true, "forceAllTransforms": true, useBuiltIns: 'usage', corejs: 3 }] ], plugins: [ '@babel/plugin-syntax-dynamic-import', [ '@babel/plugin

Function.prototype.toString issues in IE11 Svelte/Babel/Rollup

你离开我真会死。 提交于 2021-02-08 07:19:37
问题 My current rollup.config.js is commonjs(), babel({ extensions: ['.js', '.mjs', '.html', '.svelte'], runtimeHelpers: true, exclude: ['node_modules/@babel/**', 'node_modules/core-js/**' ], // <= /!\ NOT 'node_mobules/**' presets: [ ['@babel/preset-env', { // adapter to ensure IE 11 support targets: '> 0.25%, not dead, IE 11', "modules": false, "spec": true, "forceAllTransforms": true, useBuiltIns: 'usage', corejs: 3 }] ], plugins: [ '@babel/plugin-syntax-dynamic-import', [ '@babel/plugin

Sapper/Svelte possible to conditionally import components?

半腔热情 提交于 2021-02-08 06:09:54
问题 In Sapper I am trying to import a component only if being rendered client side (using onMount ). Is there something similar to React Suspense and React.lazy ? Or is there another approach? 回答1: You can certainly do it that way, yes: <script> import { onMount } from 'svelte'; let Thing; onMount(async () => { Thing = (await import('./Thing.svelte')).default; }); </script> <svelte:component this={Thing} answer={42}> <p>some slotted content</p> </svelte:component> Demo here. Alternatively, you

Svelte: Event forwarding with dispatcher vs passing in handling function, which is best practice?

…衆ロ難τιáo~ 提交于 2021-02-07 20:23:29
问题 Let's say an Outer component contains an Inner component, and we want an event from the Inner component to be propagated to the Outer component. Without using the store, there are 2 ways to do this: Method 1: Event forwarding using dispatcher Inner.svelte: Use Svelte's dispatcher to dispatch a repackaged version of the original event: <input type="text" on:input={callDispatcher} /> const dispatcher = createEventDispatcher(); function callDispatcher(e) { dispatcher("mymsg", { foo: e.target

Bind <svelte:window> to `onbeforeunload`

别来无恙 提交于 2021-02-07 10:21:08
问题 I was hoping to bind to <svelte:window> but have no luck. <!-- doesn't work --> <svelte:window on:beforeunload={() => true} /> <!-- doesn't work --> <svelte:window on:onbeforeunload={() => true} /> <!-- doesn't work --> <svelte:window on:beforeUnload={() => true} /> in all instances, window.onbeforeunload is null I ended up just going with window.onbeforeunload = () => true but was wondering why setting on the element didn't work. 回答1: You need to set the event returnValue when using svelte