Skip to content

Instantly share code, notes, and snippets.

@kuc-arc-f
Last active Jun 12, 2021
Embed
What would you like to do?
sveltekit sample, cms display
import Config from '../../../app_config'
// GET /xxx.json
export const get = async (request) => {
var cfg = Config.get_config()
const res = await fetch( cfg.MY_JSON_URL);
const posts = await res.json();
if (res.status === 404) {
return { body: [] };
}
var ret = {
status : 200,
body : posts,
}
return ret;
};
<script context="module">
import { enhance } from '$lib/form';
import Config from '../../../app_config'
// see https://kit.svelte.dev/docs#loading
export const load = async ({ fetch }) => {
const res = await fetch('/posts.json');
if (res.ok) {
const posts = await res.json();
return {
props: { posts: posts.items }
};
}
const { message } = await res.json();
return {
error: new Error(message)
};
};
</script>
<script>
import { scale } from 'svelte/transition';
import { flip } from 'svelte/animate';
export let posts;
//var cfg = Config.get_config()
// console.log(cfg)
</script>
<style>
.body_wrap{ position:relative; }
.badge_post{
position:absolute; top:-15px; left:10px;
}
</style>
<svelte:head>
<title>Posts</title>
</svelte:head>
<!-- -->
<div class="body_main_wrap">
<h1>Posts</h1>
<hr />
{#each posts as item}
<a href={`/posts/${item.save_id}`}><h3>{item.title}</h3>
</a>
ID : {item.id}
<a href={`/posts/${item.save_id}`} class="btn">[ show ]
</a>
<hr />
{/each}
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment