If you have an object of properties, you can 'spread' them onto a component instead of specifying each one:
<Info {...pkg} />
Conversely, if you need to reference all the props that were passed into a component, including ones that weren't declared with
export
, you can do so by accessing$$props
directly. It's not generally recommended, as it's difficult for Svelte to optimise, but it's useful in rare cases.
App.svelte
Info.svelte
99
1
2
3
4
5
6
7
8
9
10
11
12
13
›
⌄
⌄
<script>
import Info from './Info.svelte';
const pkg = {
name: 'svelte',
version: 3,
speed: 'blazing',
website: 'https://svelte.dev'
};
</script>
<Info name={pkg.name} version={pkg.version} speed={pkg.speed} website={pkg.website} />
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
›
⌄
⌄
⌄
⌄
⌄
⌄
⌄
⌄
⌄
⌄
⌄
⌄
⌄
/* App.svelte generated by Svelte v4.2.19 */
import {
SvelteComponent,
create_component,
destroy_component,
init,
mount_component,
noop,
safe_not_equal,
transition_in,
transition_out
} from "svelte/internal";
import "svelte/internal/disclose-version";
import Info from './Info.svelte';
function create_fragment(ctx) {
let info;
let current;
info = new Info({
props: {
name: /*pkg*/ ctx[0].name,
version: /*pkg*/ ctx[0].version,
speed: /*pkg*/ ctx[0].speed,
website: /*pkg*/ ctx[0].website
}
});
return {
c() {
create_component(info.$$.fragment);
},
m(target, anchor) {
mount_component(info, target, anchor);
current = true;
},
p: noop,
i(local) {
if (current) return;
transition_in(info.$$.fragment, local);
current = true;
},
o(local) {
transition_out(info.$$.fragment, local);
current = false;
},
d(detaching) {
destroy_component(info, detaching);
}
};
}
function instance($$self) {
const pkg = {
name: 'svelte',
version: 3,
speed: 'blazing',
website: 'https://svelte.dev'
};
return [pkg];
}
class App extends SvelteComponent {
constructor(options) {
super();
init(this, options, instance, create_fragment, safe_not_equal, {});
}
}
export default App;
result = svelte.compile(source, {
generate:
css:
});9
1
›
/* Add a <style> tag to see compiled CSS */