Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

import into the javascript frontend(html javascript)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First frontend = browser, backend = nodejs. You surely mean that LowDB is available as a NodeJS module, which means it can be installed with NPM, either for frontend or backend development.

If you're not using NPM for your frontend development, you can still import modules with <script> tags.

Let's see an example using only HTML/javascript:

(from the doc):

<html>
    <head>
        <script src="https://unpkg.com/lodash@4/lodash.min.js"></script>
        <script src="https://unpkg.com/[email protected]/dist/low.min.js"></script>
        <script src="https://unpkg.com/[email protected]/dist/LocalStorage.min.js"></script>
    </head>
    <body>

        <button onclick="add()">Add Post</button>
        <button onclick="load()">Load Post</button>
        <div></div>
    </body>
    <script>
        var adapter = new LocalStorage('db')
        var db = low(adapter)
        db.defaults({ posts: [] })
            .write()
        function add() {
          // Data is automatically saved to localStorage
          db.get('posts')
            .push({ title: 'lowdb' })
            .write()
        }
        function load() {
          var div = document.querySelector('div')
          var text = div.innerText;
          text += db.get('posts').map(post=>post.title).join(';');
          div.innerText = text 
        }
    </script>

</html>

See this work: https://codepen.io/bcaure/pen/ExgBPBz


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...